Listing Child pages with Excerpt
For those of you who use Wordpress as a CMS, I found myself searching for a way to list several related pages with an excerpt. I read through this article by The Wordpress Guru, and found that it basically described what I wanted to do, however it used calls to the database that I didn’t feel was the best way to do it. Wordpress should have something built in to do this, right? Right.
My searches through the Wordpress Codex left me a little overwhelmed. I could list all pages using get_children(), but if I tried to retrieve the title or excerpt it would only retrieve the parent posts data. So after a little more searching and testing I found the best way, that I know of, to do this:
<?php
// Set up the arguments for retrieving the pages
$args = array(
'post_type' => 'page',
'numberposts' => -1,
'post_status' => null,
// $post->ID gets the ID of the current page
'post_parent' => $post->ID,
'order' => ASC,
'orderby' => title
);
$subpages = get_posts($args);
// Just another Wordpress Loop
foreach($subpages as $post) :
setup_postdata($post);
?>
<h4><a href="<?php the_permalink(); ?>"
id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</a>
</h4>
<?php the_excerpt(); ?>
<?php endforeach; ?>
To utilize this feature this code will be placed in your template. For me I put this in page.php just above the call to get_footer();
The reason I did this is so that if I create additional pages that will contain children this list will come after the content on those parent pages. However, you can put this above ‘The Loop’ as well if you want this list to appear above your content.
If you have any questions about how this works or how to utilize this script, please post a comment. Again this is my first how-to, so if I left out something major politely let me know and I will fix it.

8 Comments to “Listing Child pages with Excerpt”