Hi
Im trying to build a customizable mega drop down (example) with get_pages and get_categories
Does anybody have a good solution for controling the depth for get_pages and get_categories?
Help would be greatly apprechiated.
The reason im not using list_pages / cats /wp_menu etc is that I want to be able to control the output to do things like top a menu with the category discription.
Pages:
Parents work the way i want. However
$subpages = get_pages('child_of='. $page->ID);
foreach ($subpages as $subpage);
Only gives me the lowest level (grandchildren).
Is there someway to set the depth?
Categories is quite simualar exept here i get both children and grandchilden.
<ul id="topnav">
<li><a href="<?php echo get_option('home'); ?>/" class="on">Home</a></li>
<?php // Create page navigation with supages in a contatiner
$pages = get_pages( 'child_of=0&parent=0');
$pageID = $page->ID;
foreach($pages as $page) {
$subpages = get_pages('child_of='. $page->ID);
if (count($subpages)>0){ // If the subpage has children give it the class hasChild and don´t close the LI ?>
<li class="hasChild"><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a>
<?php } else { // If the page does´nt have any childen. ?>
<li><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></li>
<?php } //end else ?>
<?php // If has subpages
if (count($subpages)>0){
echo "<div class='sub'><ul>";
// Generate subpages in sublevel - !!! THIS RETURNS ONLY THE LOWEST LEVEL PAGE !/%#&%!
foreach ($subpages as $subpage);{?>
<li><a href="<?php echo get_page_link($subpage->ID) ?>"><?php echo $subpage->post_title ?></a></li>
<?php }
//end foreach subpage ?>
<?php echo "</ul></div></li>";
// Close the Child UL DIV and parent LI ?>
<?php } //end if ?>
<?php } //end foreach ?>
<?php // Create category navigation with posts for each subcat
$categories = get_categories();
$catID = $cat->cat_ID;
foreach ($categories as $cat) {
if($cat->category_parent == 0){ //If is a top level category
$subcategories = get_categories('child_of='. $cat->cat_ID);
if (count($subcategories)>0){ ?>
<li class="haschild"><a href="<?php echo get_category_link( $cat->cat_ID); ?>"><?php echo $cat->name; ?></a>
<?php } /* end if has subcats */
else { // else; does not have childen ?>
<li><a href="<?php echo get_category_link( $cat->cat_ID); ?>"><?php echo $cat->name; ?></a></li>
<?php } //end else ?>
<?php // Get the subcats !!! THIS RETURNS ALL DECENDING LEVELS.
$subcategories = get_categories('child_of='. $cat->cat_ID);
if (count($subcategories)>0){
echo "<div class='sub'>";
foreach ($subcategories as $scat) { ?>
<ul>
<li><h2><a href="<?php echo get_category_link( $scat->cat_ID); ?>"><?php echo $scat->name; ?></a></h2></li>
<?php
query_posts('cat='.$cat->cat_ID); // Get posts from that category.
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; endif; //end foreach subcat ?>
<?php
echo "</ul>";
}
echo "</div> </li>"; // Close the Child UL DIV and parent LI
} //endif has subcats ?>
<?php } //end if parent ?>
<?php } //end foreach ?>
</ul> <!-- topnav -->