Kermode

/ˈkɜːrmoʊd/
The Spirit Bear

Written by Gray Gilmore

I make websites for a living. I love CSS and building simple and modular stylesheets. Somewhere along the way I also turned into a Ruby developer. Life comes at you fast I guess. You can read my resume to learn about my journey so far.

You can find me as @graygilmore on several platforms:

Good ol' fashion email works, too: hello@kermode.co

You can subscribe to this blog via RSS: XML, JSON

WordPress: Create a "Child Pages" Shortcode

It’s often the case that a client will forget to write content for a parent page – they are usually much too eager to get writing about the specifics of their website! This is understandable but doesn’t mean we should have a blank parent page.

Lately I’ve been implementing a shortcode into projects and suggest to clients that if they are struggling to find the words for a parent page to do the following:

Create a few sentences of introductory text that summarizes the section your user has just entered. To provide a little more structure and ease of navigation to the user, you can add the ‘shortcode’ [children] at the bottom of your content. This will add a bulleted list of child pages automatically for you.

One reason for the shortcode is that, quite frankly, it’s a pain in the ass to create this list manually. There’s a lot of linking to do and what happens when a new child page gets added? Chances are the parent page listing won’t be updated. This is a simple, quick way to fix that!

To the Code, James!

For this little snippet you’ll want to open up your functions.php file (if you don’t already have one in your theme, feel free to create one). All you’ll need to do is add this little bit of code:

function child_pages_shortcode() {
  global $post;
  return '<ul class="childpages">'.wp_list_pages('echo=0&depth=1&title_li=&child_of='.$post->ID).'</ul>';
}
add_shortcode('children', 'child_pages_shortcode');

I would love to give credit to where I found this, but honestly it’s been too long. If somebody knows where I snagged this from, feel free to post in the comments and I’ll add the link as soon as possible!