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:

  $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
  if ($children) { ?>
  <?php echo $children; ?>
  <?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):

// loop through the sub-pages of your custom post type
$childpages = new WP_Query( array(
'post_type' => 'work',
'post_parent' => $this_page,
'posts_per_page' => 100,
'orderby' => 'menu_order'
while ( $childpages->have_posts() ) : $childpages->the_post(); ?>
<h2 class="title"><a href="#"><?php the_title(); ?></a></h2>
<div class="content">
<?php the_content();?>
<?php $this_subpage=$post->ID; ?>
//Loop through the sub-pages of the child pages next
$subpages = new WP_Query( array(
'post_type' => 'work',
'post_parent' => $this_subpage,
'posts_per_page' => –1,
'orderby' => 'menu_order'
while ( $subpages->have_posts() ) : $subpages->the_post(); ?>
<section class="subpages">
<h3><a href="#"><?php the_title(); ?></a></h3>
<?php the_content();?>
<?php endwhile; wp_reset_query(); ?>
</div><!–.content –>
<?php endwhile; wp_reset_query(); ?>

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

  1. Found it very usefull on my blog thank you

  2. wow! neatly. going to try.

Leave a Reply

%d bloggers like this: