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.)
Otto says
Brilliant. Love it.
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.
jesse22 says
now u missed ?>
steph says
Thanks! It’s all sorted out now.