Sillybean

Listing child pages with a shortcode

If you don’t want to list child pages on all your empty pages using a page template or a filter, 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.

Posted on June 8, 2010 at 10:43 am in WordPress · 5 comments

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

5 Responses to “Listing child pages with a shortcode”

  1. 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!

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

  3. 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');

  4. 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?

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>