Custom Blog Designs by Blog Theme Machine

Listing Wordpress Pages Multiple Levels Deep

I was recently working on a client project that required the ability to show a “sub menu” when you’re on a specific page and also the sub-sub page for the first sub page. So for instance I have the following:

  1. HOME PAGE
  2. HOME PAGE – FIRST PAGE
  3. HOME PAGE – FIRST PAGE – SECOND PAGE

Now, I needed to be able to call a list for the FIRST PAGE to showcase any of the SECOND PAGE links, but also pull a list on the SECOND PAGE for anything under that one (ie: THIRD PAGE). I found a quick solution to show sub pages by using a simple piece of php code, but that wouldn’t work for a SUB – SUB PAGE, thus, after some digging and piecing together, the code below works like a charm.

<?php
global $post;

if ( $post->post_parent )
$parent = wp_list_pages(”title_li=&echo=0&hierarchical=0&include=” . $post->post_parent); else

$thispage = wp_list_pages(”title_li=&echo=0&hierarchical=0&include=” . $post->ID);
$children = wp_list_pages(”title_li=&echo=0&depth=1&sort_column=menu_order&child_of=” . $post->ID);

if ($children) { ?>
<div class=”sidebox”>
<img src=”<?php bloginfo(’stylesheet_directory’); ?>/images/side_subnavigation.jpg” alt=”Navigation” />
<ul>
<?php echo $children; ?>
</ul>
</div>
<? } ?>

Now you’ll notice that I added a <div> around my ul list because thats what my design called for but you can remove the div, change it to fit your needs, or style the ul by adding a class to it, ect. That is a whole other subject, but for now, at least I have a working piece of php code that will display a SUB navigation and also a SUB SUB navigation when needed on the site.

*Please note due to the way that the blockquotes work in wordpress, the above code will need the ” replaced. Just highlight each ” and retype it out yourself once you paste the code into whatever editor you’re using.

Speak Your Mind

Custom Blog Designs by Blog Theme Machine
© 2007-2010 Powered by Wordpress & Made by Guerrilla.