Theme file for all pages that are a child of a specific page

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 Comment