WordPress Snippets at WPcustoms

Dynamically use parent template for child pages

If you use a specific template for a parent page this function automatically uses this template for the page’s child pages. No manual updating required. The code checks the page template meta field when you save a post and if it has a parent page it uses the page template from that one.


/**
 * Snippet Name: Dynamically use parent template for child pages
 * Snippet URL: https://wpcustoms.net/snippets/dynamically-use-parent-template-for-child-pages/
 */
  function wpc_use_parent_template(){
    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);
    }
}
add_action('save_post','wpc_use_parent_template');