If you don’t want to list child pages on all your empty pages using a [link id=”2865″]page template[/link] or a [link id=”5246″]filter[/link], you can create a simple shortcode:
function child_pages_shortcode() { global $post; return '<ul class="childpages">'.wp_list_pages('echo=0&depth=0&title_li=&child_of='.$post->ID).'</ul>'; } add_shortcode('children', 'child_pages_shortcode');
Then place [[children]] in your page content where you want the list to appear.
Demetris says
Making this into a shortcode or a plugin is a clever idea!
I use myself the IF EMPTY, SHOW CHILDREN method in a couple of themes (including the one I showed you the other day) but I had never thought of that alternative.
Cheers!
Jon says
Thanks for this.
I’d may be helpful to others if you clean up the HTML formatting/escaping in the code snippet.
steph says
Thanks for letting me know the code had gotten mangled! That happened to a bunch of posts over the summer when I ran a batch job on my database. I thought I’d fixed all the code snippets, but I probably need to check them all again.
Jon says
I should of added, this is the code I ended up with (note I changed the shortcode name)
// Child Page Shortcode
function child_pages_shortcode() {
global $post;
return ''.wp_list_pages('echo=0&title_li=&child_of='.$post->ID).'';
}
add_shortcode('child_pages', 'child_pages_shortcode');
Miko says
Thanks for the code, it’s great but I am trying to get the child pages to show in reverse chronological order (newest first, by post date). I tried adding this:
&sort_column=post_date
to the wp_list_pages bit, but it doesn’t seem to work. Any clue why not?
Brandon says
Is it possible to sneak an attribute into wp_list_pages? So for example if you wanted to list the pages of a certain parent you could specify the parent in the shortcode like this [children parent=”5″]
Stephanie Leary says
Sure. Your function would look something like this:
function child_pages_shortcode( $atts ) {
$a = shortcode_atts( array(
‘parent’ => get_the_ID(),
), $atts );
return ‘<ul class="childpages">’.wp_list_pages(‘echo=0&depth=0&title_li=&child_of=’.$a[‘parent’] ).'</ul>’;
}
See the Shortcode API for more info. Or you could use the Display Posts Shortcode plugin, which has a ton of options like this built in.