• 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

Listing child pages with a shortcode

June 8, 2010 Stephanie Leary 7 Comments

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.

WordPress Posts and Pages, shortcodes

Reader Interactions

Comments

  1. Demetris says

    June 9, 2010 at 9:19 am

    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!

    Reply
  2. Jon says

    September 20, 2010 at 4:26 pm

    Thanks for this.

    I’d may be helpful to others if you clean up the HTML formatting/escaping in the code snippet.

    Reply
    • steph says

      September 22, 2010 at 10:12 pm

      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.

      Reply
  3. Jon says

    September 20, 2010 at 4:33 pm

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

    Reply
  4. Miko says

    April 16, 2011 at 11:47 am

    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?

    Reply
  5. Brandon says

    December 3, 2014 at 8:47 pm

    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″]

    Reply
    • Stephanie Leary says

      December 4, 2014 at 2:05 pm

      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.

      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