Stephanie Leary

Writer and WordPress consultant

  • Books
    • Content Strategy for WordPress (2015)
    • WordPress for Web Developers (2013)
    • Beginning WordPress 3 (2010)
  • Blog
    • Fascism Watch (2016-17)
    • Content Modeling for WordPress series
    • WordPress Hidden Gems series
  • Work
    • Portfolio
    • Services
    • WordPress Plugins
    • WordPress Themes
    • Presentations and Interviews
    • on GitHub →
  • About
    • Press Kit
  • Contact
    • Mailing List

Listing child pages

August 9, 2009 Stephanie Leary 2 Comments

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

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

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

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

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.

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

Example: table of contents

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

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

WordPress Navigation, pages, Posts and Pages, template tags

Comments

  1. Brian says

    January 15, 2010 at 8:20 pm

    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.

    Reply
  2. Stephanie Leary says

    February 9, 2010 at 8:11 pm

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

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Fascism Watch

The Fascism Watch is a daily(ish) news roundup. View all the previous Fascism Watch posts »

Latest WordPress Book

Content Strategy for WordPress

A short book for content strategists and managers on implementing a complete content strategy in WordPress: evaluation, analysis, content modeling, editing and workflows, and long-term planning and maintenance.

Read the sample chapter

Kindle Nook iBooks Kobo Smashwords

WordPress for Web Developers

WordPress for Web Developers (9781430258667)

This is a book for professional web designers and developers who already know HTML and CSS, and want to learn to build sites with WordPress. The book begins with a detailed tour of the administration screens and settings, then digs into server-side topics like performance and security. The second half of the book is devoted to development: learning to build WordPress themes and plugins.

This is the second, much-revised and updated edition of Beginning WordPress 3, with a more accurate title. Everything’s been updated for WordPress 3.6.

WordPress for Web Developers is out now. See what's inside...

The best WordPress features you’ve never noticed

  • WordPress Hidden Gems: Screen Options
  • WordPress Hidden Gems: Bulk Edit
  • WordPress Hidden Gems: Private Status
  • WordPress Hidden Gems: Dashboard Feed Readers
  • WordPress Hidden Gems: Options.php

Content Modeling for WordPress series

  • Content modeling for WordPress, part 1: analyze content
  • Content modeling for WordPress, part 2: functional and organizational requirements
  • Content modeling for WordPress, part 3: a sample content model

This is an excerpt from Content Strategy for WordPress.My latest books are Content Strategy for WordPress (2015) and WordPress for Web Developers (2013). Sign up to be notified when I have a new book for you.

Copyright © 2021 Stephanie Leary