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 <php
if( is_page() )
{
wp_list_pages('child_of=' . $wp_query->query_vars['page_id']);
}
?>
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->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.