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.

How to assign parent page template to its child pages dynamically?

share|improve this question

2 Answers 2

A little correction to the Max Yudin's solution:

add_action('save_post','changeTemplateOnSave');
function changeTemplateOnSave(){
    global $post;
    $curr_tmp = get_post_meta($post->ID, '_wp_page_template', true);
    if($post->post_parent){
        $parent_tmp = get_post_meta($post->post_parent, '_wp_page_template', true);
        update_post_meta($post->ID,'_wp_page_template',$parent_tmp,$curr_tmp);
    }
}
share|improve this answer

Paste following code to your theme's functions.php:

add_action('save_post','changeTemplateOnSave');
function changeTemplateOnSave(){
    global $post;
    $curr_tmp = get_post_meta($post->ID, '_wp_page_template', true);
    $parent_tmp = get_post_meta($post->post_parent, '_wp_page_template', true);
    if($post->post_parent)
        update_post_meta($post->ID,'_wp_page_template',$parent_tmp,$curr_tmp);
}

This will force WordPress to change page template to it's parent template on post save.
Not tested but should work.

share|improve this answer
    
Thanks @Max Yudin for your help –  Krishnendu Sep 26 '12 at 12:03
    
If the code is working, please mark it as the accepted by clicking on the check box outline to the left of the answer. –  Max Yudin Sep 26 '12 at 12:52

Your Answer

 
discard

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.