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

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

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 *

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