Recent Tweets

How To List WordPress Child Pages Only On That Parent Page

How To List WordPress Child Pages Only On That Parent Page

Last Updated: September 3, 2009 2:56 pm 30 comments

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
        <li>page 1</li>
        <li>page 2</li>
        <li>page 3</li>
        <li>page 4</li>
        <li>page 5</li>

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() ) {
      $children = wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->post_parent.'&echo=0');
      $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>
      	<?php echo $children; ?>
} // 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..

The following two tabs change content below.


Web Developer
Senior Web Developer who combines passionate coding skills, strategic vision with strong hands on leadership and enormous personal drive. I create re-usable industry standard code that is used time and time again, producing outstanding results for every project I work on! I also like Classic Motorbikes, Quad bikes, Downhill Mountain biking & Learning electronics using the Arduino.

Latest posts by Marty (see all)