Sillybean

Automatically list children on empty pages

Here’s a little snippet I use in my theme’s page templates. It figures out whether a page’s content is empty, and if it is, a list of all its child pages is printed where the content would usually go.

<?php
$content = get_the_content();
if (empty($content)) {
	$children = wp_list_pages('title_li=&depth=1&child_of='.$post->ID.'&echo=0&sort_column=menu_order');
	if ($children) { ?>
<ul id="childpages">
		<?php echo $children; ?>
		</ul>
<?php }
}
else the_content();
?>

To use this, find the_content() in your page.php file and replace it with that chunk of code. Here, I’m listing only one level of child pages (depth=1). Remove or amend that number if you want to list grandchildren as well.

I’ve written before about several other examples of listing child pages. I don’t think page lists are going to go away now that we have menus in 3.0. Listing children automatically is just too useful!

This is one of the tricks from the theme chapters in Beginning WordPress 3.

(See this as a filter rather than a theme file snippet.)

Posted on June 2, 2010 at 11:38 am in WordPress · 5 comments

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

5 Responses to “Automatically list children on empty pages”

  1. Otto says:

    Brilliant. Love it.

  2. Otto says:

    Warning, typo. You’re missing a closing } bracket somewhere.

    • steph says:

      Thanks, Otto! Fixed. Sorry about that — I had to redo the code about twelve times, because I’m having trouble with the syntax highlighters for some reason and they keep converting things to HTML entities.

  3. jesse22 says:

    now u missed ?>

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>