Show WordPress Child Pages Alongside Parent Page

I recently needed some subpage code for a WordPress project. Darren Hoyt had a quick and easy solution to show the child pages of a parent page, only if the page had subpages. This was useful. My project needed to include the parent page at the top of the list of child pages, so I’ve added a small modification to it.

This goes in your page template or sidebar template where you want your submenu to appear. It is outside the loop.

<?php
 $parent_title = get_the_title($post->post_parent);
 if($post->post_parent)
 $children = wp_list_pages("title_li=&sort_column=menu_order&child_of=".$post->post_parent."&echo=0&depth=1"); else
 $children = wp_list_pages("title_li=&sort_column=menu_order&child_of=".$post->ID."&echo=0&depth=1");
 if ($children && is_page()) { ?>
     <ul class="menu">
       <li <?php if ( is_page() && $post->post_parent ) {} else { ?>class="current_page_item"<?php } ?>><a href="<?php echo get_permalink($post->post_parent) ?>"><?php echo $parent_title;?></a></li>
          <?php echo $children; ?>
     </ul>
 <?php } ?>

As it stands, the only issue with this code, is that when you are on the parent page, the class that identifies it as current does not appear. If you know of a solution for this, please do tell.

Updated Oct 7th, 2011 to include “current” state on parent page.

2 Comments
  1. Raffi Darrow August 4, 201111:18 am

    Try this?
    http://pastebin.com/YrJwHAzW
    I’m using it already – on one site it works fine. On the other I am having trouble with the parents making space for the children.

  2. Chad Von Lind October 7, 20114:03 pm

    Thanks Raffi. I finally had a chance to play with your supplied code, but it only seemed to output the entire structured page list, and didn’t really do much else.

Submit Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

To include your code, use pastebin, and paste the URL in the comment body. Failure to do so may prevent your comment from appearing. Thank you.