Dynamically List Sub-Pages

Posted on May 27, 2011 in WordPress | 0 comments

When to List Child Pages on a Page

Many prominent sites, even those with a significant amount of content, have only a few prominent navigation links on their home page. When you hover over these, a list of subpages appears: it keeps the layout compact and simple, while allowing quick access to a large number of pages.

However, the hover nicety doesn’t always work. Most notably, smartphones have no way to hover: it’s click, or nothing. Typically, you want that main hover link to actually go somewhere, even if it’s just a simple page with a link to all the subpages.

There are other times you want to list the children of a page, of course. But no matter the reason, you want to avoid hard-coding a list of links on that page because, if you’re like me, you just <em>know</em> that the next time you add or remove a page, you’ll forget to change the links in that list. It’s so much easier to keep it automatic.

So if you have a page “Content Management Systems,” how do you get the child pages “WordPress,” “Joomla” and “Drupal” to be listed as links on that page?

Listing Pages in the Template

The WordPress Codex describes how to use the wp_list_pages function to print out a list of links to any given set of pages. If you want to hard-code that into the PHP, that’s all well and good. Perhaps you want to create a page template for pages that will list their children at the end of the page. If you want to just use a shortcode to add that capability to individual pages without modifying the underlying PHP, then you need a plugin.

List Pages Shortcode Plugin

If you’ve ever wanted to automatically have a list of pages appear on your website, but don’t want to delve into the PHP code, then the List Pages Shortcode plugin is for you.

It’s simple and it works. There’s no configuration. Just install and activate, then use the shortcodes described in the List Pages Shortcode documentation. Most commonly, I just add this to a page:

[child-pages depth="1"]

And that will list links to that page’s children (but not its children’s children). Just as easy as that.

Submit a Comment

Manage your comment subscriptions