Creating sidebar navigation using wp_list_pages
-
Hi,
I’m building a sidebar navigation to list pages and subpages and using wp_list_pages.
My menu structure contains parent pages and 2 levels of subpages (children):
Cars from Europe
– Germany
— Volkswagen
— Audi
– France
— Citroen
— Peugeot
– ItalyI want this:
- On a parent page only it’s children should be displayed (Cars from Europe: Germany, France, Italy).
- On a childpage it’s children and it’s siblings should be displayed (Germany: Volkswagen, Audi (= children) and France, Italy (= siblings).
I use this to make it work:
$current = '<li class="current"><a href="'.get_permalink( $post->ID).'">'.get_the_title( $post->ID).'</a></li>'; $children = wp_list_pages( array( 'title_li' => 0, 'child_of' => $post->ID, 'depth' => 1, 'echo' => 0 ) ); $siblings = wp_list_pages( array( 'title_li' => 0, 'child_of' => $post->post_parent, 'depth' => 1, 'echo' => 0 ) ); <?php if ( $post->post_parent ) { ?> <?php echo '<div class="sidebar-nav"><ul>'.$current.$children.$siblings.'</ul></div>'; ?> <?php } else { ?> <?php echo '<div class="sidebar-nav"><ul>'.$current.$children.'</ul></div>'; ?> <?php } ?>
Variable $current = current page (I want to list current pagename before the other pages. So I hide it via CSS in the $siblings list).
I did make it work but it’s not very clean coding, so who can help me to clean up this?
Guido
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘Creating sidebar navigation using wp_list_pages’ is closed to new replies.