WordPress Snippet: show all child pages of a page

Here is a pretty handy snippet that will display all the child pages of the current page:

<?php
  $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
  if ($children) { ?>
  <ul>
  <?php echo $children; ?>
  </ul>
  <?php } ?>

Maybe you have a page called “Services” and you want it display a list of services. Instead of making a long page that’s hard to update, you could just add a child page for each of the services and this will automatically create a list out if it.

You could also use this to display pages on a sidebar instead of having a Custom Menu for each page.

Another cool similar usage of this I’ve seen is displaying all child pages in accordions! You can see Christine Rondeau’s gist of how she does that here (note that she uses a Custom Post Type in this example):

2 Replies to “WordPress Snippet: show all child pages of a page”

Leave a Reply