WordPress FAQ: How Do I Use Child Pages More Effectively?

Hidden child pages on front page of site in new version of wordpress… any simple tutorial on making this happen or any plans on making this a function of wordpress in the future? It would really help wordpress have better functionality for those of us who also use it as a CMS.
<snip>
I know it’s more of a templating issue. By default most templates place all pages at the top or sidebar. Well, truth is for organization sake, this is not always good. I like to be able to link to these (which is simple enough) but only have them display that link on the parent page rather than the front page (meaning “front” of blog”). This would be a great “built-in” functionality, but right now it’s not there.

This question comes from William Lehman and is fairly involved. In fact, several exchanges occurred before I completely understood what William was driving at. In essence, the issue is that “Child Pages” – that is pages that have been assigned a Parent Page – display on all pages when using the template tag

1
<a href="http://codex.wordpress.org/Template_Tags/wp_list_pages">wp_list_pages</a>

and he wants to only list a child page if it is the parent page that the reader is on.

Complex, right?

Not so much with a little Template Grease™

Let me provide a little pseudo code. As templates are very individual things, where exactly this is placed is completely dependent on where you want your code to be displayed and in what form you want it to be displayed in.


1
2
3
4
5
6
&lt;php
if( is_page() )
  {
  wp_list_pages('child_of=' . $wp_query-&gt;query_vars['page_id']);
  }
?&gt;

Okay, complex right? The bark is worse than the bite. First we do a little fact-checking. Are we on a page or not? This page load isn’t actually a feed? …Or an archive page? …or just a simple post? Right? Okay, then we move on.

Every page load in WordPress carries with it a set of query variables. No one ever sees these unless you go looking for them, but the query variables dictate how the page is loaded, if theres anything special that the rest of WordPress has to consider, such as categories or dates, or, if the page load is a page or a post. Having determined already that your page is actually a page and not a post, we can tap into the

1
$wp_query

object and extract the page ID from the query variables.

Having done this, we can pass this ID with it’s ubiquitous reference of

1
$wp_query-&gt;query_vars['page_id']

to the

1
wp_list_pages

template tag using the “child_of” argument. This argument tells the page list creator that we only want to find the subpages of this particular page that we’re on.

And that’s it. Naturally, you can explore the deepr treasures of

1
<a href="http://codex.wordpress.org/Template_Tags/wp_list_pages">wp_list_pages</a>

and style your navigation/page list accordingly. That should get you going in the right direction though.

Readers are welcome to send their questions in to me and I’ll do my best to give them a spin.

Aaron Brazell

Aaron Brazell is a Baltimore, MD-based WordPress developer, a co-founder at WP Engine, WordPress core contributor and author. He wrote the book WordPress Bible and has been publishing on the web since 2000. You can follow him on Twitter, on his personal blog and view his photography at The Aperture Filter.

9 thoughts on “WordPress FAQ: How Do I Use Child Pages More Effectively?

  1. Interesting. This would have useful application for category archives as well, especially for those of us who liberally use sub-categories as a means of tagging.

  2. I still think tagging is a better way to go for most situations, but I guess there is a fringe in the WP as a CMS world that would use page/subpage/hidden/visible concept as a structural tool. In that case, this would be a more semantic solution.

  3. What advantages obtain from direct tagging, whether manually or by widget, vice using the WP category?

    As best I can tell, Technorati is picking them up just fine as a “tag” and I seem to score relatively high in the search engines. Of course, I don’t have an identical version of the weblog running using direct tagging as a counterpoint, so I don’t know how to test this.

  4. `wp_list_pages(‘child_of=’ . $wp_query->query_vars['page_id']); `could be replaced by :
    ` wp_list_pages(‘child_of=’.$post->ID); `

    KISS ;)

  5. Any ideas on how to create a (sub-)page containing a list of its child pages including not just the header, but also (part of) the child page content?

    Thanks!

  6. Hi,

    second Malaiac’s post, but wanted to ask if there is any difference really, other than it is more “readable”?

    k.

  7. Yes, navigation to those childpages was a problem when I was adding the static content of my former website as pages to my blog.
    And I solved it by using Ryan’s Suckerfish dropdown menu plugin,that made it possible to display links to only the parent pages in the navigation bar, but with dropdown-menus with the child pages when hovering over them.

    This makes the navigation even more direct than your solution does, without cluttering the page with lots of links.

Comments are closed.