Sillybean

Listing child pages

The Codex offers a way to list the children of the current page by adding this to your theme:

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

… or listing the siblings of the currently viewed child page:

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

However, if you don’t want to include the code in your template file, you can use the shortcodes plugin for listing pages and child pages.

The above code can be expanded in a number of ways….

Example: excerpts of top-level pages with linked subpages

Here, we want to show excerpts from our top level pages, and under that, a list of the subpages. We need PJW Page Excerpt to enable excerpts for pages.

<br />
<?php<br />
$pages = get_posts('post_type=page&#038;orderby=menu_order&#038;order=ASC&#038;post_parent=1200&#038;posts_per_page=-1');</p>
<p>foreach($pages as $post) : ?></p>
<div class='post' id='<?php echo $post->post_name; ?>'></p>
<h2><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?></p>
<p><?php<br />
$children = wp_list_pages('title_li=&#038;child_of='.$post->ID.'&#038;echo=0&#038;depth=1');<br />
if ($children) { ?></p>
<ul class='subnav'>
    <?php echo $children; ?>
  </ul>
<p><?php } ?></p>
</div>
<p> <!-- .post --><br />
<?php endforeach; ?><br />

Example: table of contents

In this example, we want to display a table of contents for all the children and grandchildren of page #1200:

<br />
<?php<br />
$pages = get_posts('post_type=page&#038;orderby=menu_order&#038;order=ASC&#038;post_parent=1200&#038;posts_per_page=-1');</p>
<p>foreach($pages as $post) : ?></p>
<ol class='post' id='<?php echo $post->post_name; ?>'></p>
<li><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a><br />
      <?php<br />
      $children = wp_list_pages('title_li=&#038;child_of='.$post->ID.'&#038;echo=0&#038;depth=1');<br />
      if ($children) { ?></p>
<ul class='subnav'>
          <?php echo $children; ?>
        </ul>
<p>      <?php } ?>
    </li>
</ol>
<p><?php endforeach; ?><br />

Posted on August 9, 2009 at 9:15 am in WordPress · 2 comments

Sharing this post? The short URL is http://sillybean.net/?p=2704

2 Responses to “Listing child pages”

  1. Brian says:

    Thanks for the post. Can you recommend a solution for merging the first two code samples? My intention is to show the children as well as the siblings of any given page.

  2. Brian, I’m sorry I overlooked your comment last month. Did you figure it out?

Leave a Reply