Check out the "Digging into wordpress book by: "by Chris Coyier and Jeff Starr."
How To List WordPress Child Pages Only On That Parent Page
Posted on September 3, 2009 by Marty
How to create a dynamic list of page title links when you are on a page that has sub pages, ie: If using a gallery for a page, were each sub-page is a gallery of images
When you have multiple pages in your wordpress website, either using it as a blog or as a complete CMS, this quick tip will help show you how to:
Only show those child pages of the parent page,
Only when you are on that Parent page!
Below is the HTML framework for our sidebar navigation…
- Gallery
- page 1
- page 2
- page 3
- page 4
- page 5
<ul>
<li>Gallery
<ul>
<li>page 1</li>
<li>page 2</li>
<li>page 3</li>
<li>page 4</li>
<li>page 5</li>
</ul>
</li>
</ul>
When on the “PARENT” page,
The code in the sidebar, navigation menu or page,
will be populated with a list of Child Sub pages for THAT current PARENT page only…
Copy the code below into your sidebar, and try it…
<?php if ( is_page() ) {
if($post->post_parent)
$children = wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->post_parent.'&echo=0');
else
$children = wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->ID.'&echo=0');
if ($children) {
?>
<div class="sidebar">
<h2>Sub-pages of Current Page</h2>
<ul>
<?php echo $children; ?>
</ul>
</div>
<?php
} // End If Post
} // End if is page
?>
As long as your Page has at least one associated child page, the list will be shown, if there are no child pages then the menu will not be shown.. this is handy if you’re sidebar is quite packed with elements/ads, this will be generated only on that parent page…
you can check out the example on the Shugtech Gallery website.
Each Gallery Album is a child of Gallery, so each image gallery is a separate wp page were the list of other Albums available is on the right hand sidebar…
As always feedback is welcome..
Comments (3)










site only begining!!!! Thansk for the code, it great, no plugin needed!!! how do you get the name of the parent page to show as well? thanks owen
This does not work for me. It shows ALL pages on the site with sub pages indented as planned, but I need a menu that shows only CURRENT parent page and sub pages. Do you know if anything changed for 2.9.2?
Thanks,
Bob
I know this is an old post, but I was looking for something similar to this in WP3.0 and came acrose Sub Pages widget. Works Great!