Is it possible within the current theme file naming hierarchy to define a template for for all pages that are a child of a specific page? For example, in this navigation:

About Us

  • Contact
  • Who We Are
  • Message Statement

Is there any way to just make a theme file named something like:

page-about-us-all.php

That would automatically be applied to all pages that are children of About Us?

UPDATE

I went with a modified version of what Bainternet had suggested. Here’s the descendant function I ended up with:

function is_descendant($ancestor, $tofind = 0) {
    global $post;
    if ($tofind == 0) $tofind = $post->ID;
    $arrpostids = get_post_ancestors($tofind);
    $arrpostslugs = array();
    foreach($arrpostids as $postid) {
        $temppost = get_post($postid);
        array_push($arrpostslugs, $temppost->post_name);
    }
    return (in_array($ancestor, $arrpostids) || in_array($ancestor, $arrpostslugs));
}
// Example use:
is_descendant('about-us');
is_descendant(123);
is_descendant('about-us', 134);

This allows me to verify it’s a descendant using either the ID of the parent or the slug. I was concerned that using only the ID might lead to a problem if a parent page had been accidentally trashed there would be no good way to get that to work again without having to edit the Theme files. With the slug at least there’s the option of just hopping in and making a new page with the same slug and hierarchy.

3 s
3

I have a handy little custom made conditional function that would do the job for you.

The function:

function is_child_page($page = NULL){
    global $post;
    if ($page == NULL){
        $p = get_post($post->ID);
        if ($p->post_parent  > 0 ){
            return true;
        }else{
            return false;
        }
    }
    $args = array( 'child_of' => (int)$page);
    $pages = get_pages($args); 
    foreach ($pages as $p){
        if ($p->ID == $post->ID){
            return true;
            break;
        }
    }
    return false;
}

Usage:

if (is_child_page()){
    //this page has a parent page
}

if (is_child_page(23)){
    //this page is a child page of the page with the ID of 23
}

Now you ask how can this help you?

After you have this function saved in your theme’s functions.php file
edit your theme’s page.php file and add at the very top something like this:

if (is_child_page(12)){
    include (TEMPLATEPATH . '/page-about-us-all.php');
    exit();
}

And you are done! Note: this code is assuming that your about page id is: 12 and your theme file is named: page-about-us-all.php.

Leave a Reply

Your email address will not be published. Required fields are marked *