6

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.

2
  • Do you want this to work for all child pages across your site, or only "About Us" child pages? Mar 28, 2011 at 21:47
  • ad is_descendant(): there are some things that should be avoided regarding speed: count, array checking and other stuff that "does" something else than just representing a value processed before, inside if/elseif/for/foreach/while - anyway: faved & +1
    – kaiser
    Apr 2, 2011 at 14:21

3 Answers 3

5

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.

3

This can be much simpler, and won't depend on separate functions.

Assuming we want to check whether the current page is a child of page 134, dropping the following into the top of your page.php would be enough:

<?php
if (134 == $post->post_parent) {
    include (TEMPLATEPATH . '/page-mysubpagetemplate.php'); // Name this for your child page template name
    exit();
} else {
    // Do something else
    // You might want to stick your regular page.php code in here, or alternatively, you could call another template
}; ?>
3
  • I believe this would only work for pages that were direct descendants. Anything further down would not pass the $post->post_parent check. May 1, 2014 at 17:55
  • @Jon Lay Is there a way to do this where you would only write code in functions.php?
    – Cody
    Jan 4, 2015 at 8:14
  • 1
    get_stylesheet_directory() is a bit safer than TEMPLATEPATH as it covers child themes too. Also, you don't need to include else, that part would be executed anyway if it doesn't meet the condition.
    – pax
    Jul 31, 2015 at 5:44
1

This is not possible, by default, in the WordPress templating system. The template hierarchy does not detect the parent-child Page relationship. However, I did find this blog post, explaining a method to automate the process. I've not tried to implement it, so I don't vouch for its effectiveness, but it is at least worth a read.

(Note: this blog post hooks its function into 'save_post', which means that Page Template switching happens only when the post is saved. This should generally work, since changing the Parent Page requires a save action. However, you may need a different hook, if you need to perform retroactive Page Template updates.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.