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

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

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 *

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