• Skip to primary navigation
  • Skip to main content
  • Skip to footer
  • Books
    • Content Strategy for WordPress (2015)
    • WordPress for Web Developers (2013)
    • Beginning WordPress 3 (2010)
  • Blog
    • Content Modeling for WordPress
    • WordPress Hidden Gems
    • Web Design
  • Work
    • MLIS Class Projects (2019-2022)
    • Portfolio (2002-2019)
    • Services
    • WordPress Plugins
    • WordPress Themes
    • Presentations and Interviews
    • on GitHub →

Stephanie Leary

Writer, WordPress consultant, recent MLIS grad

  • About
    • Press Kit
    • Presentations and Interviews
  • Contact Me

Automatically list children on empty pages

June 2, 2010 Stephanie Leary 5 Comments

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.)

WordPress Posts and Pages

Reader Interactions

Comments

  1. Otto says

    June 3, 2010 at 11:33 am

    Brilliant. Love it.

    Reply
  2. Otto says

    June 3, 2010 at 11:37 am

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

    Reply
    • steph says

      June 3, 2010 at 2:24 pm

      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.

      Reply
  3. jesse22 says

    June 29, 2010 at 3:30 pm

    now u missed ?>

    Reply
    • steph says

      June 29, 2010 at 4:16 pm

      Thanks! It’s all sorted out now.

      Reply

Leave a Reply Cancel reply

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

Footer

My Books

I’m a front end developer at Equinox OLI, working on open source library software. I was previously a freelance WordPress developer in higher education. You can get in touch here or on LinkedIn.

Copyright © 2023 Stephanie Leary · Contact