Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

As the title implies, I'm trying to set each new page under X parent to a specific page template. Either through a function/plugin/code

eg: if I make a 'swiss cheese' page, that is a children of the 'chesses' page, wp will automatically assign it the 'cheese' page template

share|improve this question

2 Answers 2

On the admin side, you could update the meta data for the page-post_type programmatically:

global $post;
if ( 
    'swiss_cheese' === $post->post_parent 
    AND is_admin()
    update_post_meta( $post->ID, '_wp_page_template', 'some_template.php' );

On the visitor facing side, you can simply jump into template redirect:

function wpse63267_template_include( $template )
    global $post;

    if ( 'swiss_cheese' === $post->post_parent )
        $new_template = locate_template( array( 'swiss-cheese-template.php' ) );
        if ( ! empty( $new_template ) ) {
            return $new_template ;
    return $template;
add_filter( 'template_include', 'wpse63267_template_include', 99 );
share|improve this answer
not sure what 'global $post' means and where would the admin part go? –  alme1304 Aug 28 '12 at 1:32
global $post is basic PHP and the admin part would go into a plugin or the functions.php file. of course, wrapped up in a function and hooked. –  kaiser Aug 28 '12 at 9:16
from the wp codex‌​: Loading a different template is not a good use of this action hook (template_redirect). If you include another template and then use exit() (or die()), no subsequent template_redirect hooks will be run, which could break the site's functionality. –  honk31 Jun 25 at 13:53
@honk31 Thanks for your help fixing a 2 year old question ;) –  kaiser Jun 25 at 14:32
@kaiser actually i'm fixing my own problems here, but since this helped me to get started i thought its always good to share the results.. <3 –  honk31 Jun 25 at 14:38

i combined the solution of kaiser with some other solution i found on the net, to make it more dynamically and bulletproof.

plus in the wp codex it says: Loading a different template using template_redirect action hook is not a good use. If you include another template and then use exit() (or die()), no subsequent template_redirect hooks will be run...

And i made this dynamic as hell. you can say: children of page X use template Z. but if you set a template for a childr of page X in the admin interface, then use this one (for exactly that child).

Here is a solution (if child of page X redirect to...)

function jnz_is_child( $pid ) {
    global $post;
    $ancestors = get_post_ancestors( $post->$pid );
    $root = count( $ancestors ) - 1;
    $parent = $ancestors[$root];
    if( is_page() && ( is_page( $pid ) || $post->post_parent === $pid || in_array( $pid, $ancestors ) ) ) {
        return true;
    } else {
        return false;
function wpse140605_template_redirect( $template ) {
    if ( $template === locate_template('page.php') ) { // if template is not set in admin interface
        if ( jnz_is_child( 656 ) || jnz_is_child( 989 ) ) {
            $new_template = locate_template( array( 'page-whatever.php' ) ); // if template file exist
            if ( '' != $new_template ) {
                return $new_template ;
    return $template;
add_filter( 'template_include', 'wpse140605_template_redirect', 99 );

So basically in the first function jnz_is_child(), I check if the page is a child of page X (or even a child of a child of X..) by page ID.

And in the second function wpse140605_template_redirect() I do the replacement (if page parent === 656 or === 989: use template page-whatever.php). Of course, the second function can contain multiple if / else if closures, plus this function checks, if the template file exists. Because if it does not exist, php will throw an error.

And the first function is also usable everywhere else inside the theme (like is_page() etc.).

share|improve this answer
I'm not really sure if the first function works in all cases. Have you actually tested it? First I wanted to recommend dropping it in favor for a check against 0 (no parent set), but then I realized that you may need it in case you have multi level hierarchy. Still not sure if it is right to assume that you count() all ancestors and take the one before the last item. What if it only returns 1? An error would be triggered. And I think this is just an assumption as the array could have gotten reshuffled, sorted, etc. –  kaiser Jun 26 at 8:46
yes. it's tested & works. this tests, if current page is child of page X. true / false is the only answer. you can even test if page is child of root (0) and it works. –  honk31 Jun 26 at 10:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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