WordPress Apply parent page template to subpage

Following code will be useful if you want to apply parent page template to all the subpages without selecting page template from the options.

You have to add following code inside the function.php you can find this file inside your themes folder.

function change_page_template(){
	global $post;
	$current_page_template = get_post_meta($post->ID,'_wp_page_template',true);
	$parent_page_template = get_post_meta($post->post_parent,'_wp_page_template',true);
	$parents = ($post->post_parent==$post->ID) ? true : false;

So after adding these lines parent page template will be added automatically to all the subpages.

6 thoughts on “WordPress Apply parent page template to subpage

  1. im getting an error on this line

    $parents = ($post->post_parent==$post->ID) ? return true : return false;

    can you advise what the fix is

    1. Yep.. don’t know why the function name I called “update_post_meta” is changed to “fun”.. code is working now. Post is updated. Copy code again from the post

  2. im still getting an error on the line before, think it has something to do with

    ? return true : return false;

    though php isnt my strong point

  3. ? return true : return false;

    should be:

    ? true : false;

    so the full code is:

    function change_page_template(){
    global $post;
    $current_page_template = get_post_meta($post->ID,’_wp_page_template’,true);
    $parent_page_template = get_post_meta($post->post_parent,’_wp_page_template’,true);
    $parents = ($post->post_parent==$post->ID) ? true : false;

  4. hmm, nevermind. Even after changing add_action(‘save_post’,’switch_page_template’); to add_action(‘save_post’,’change_page_template’); this does not appear to be working in the latest version of WP.

  5. My approach:

    $current_page_template = get_post_meta($wp_query->post->ID,'_wp_page_template',true);
    $parent_page_template = get_post_meta($wp_query->post->post_parent,'_wp_page_template',true);

    if ($parent_page_template != '')
    $template = $template = TEMPLATEPATH . "/" . $parent_page_template;


Leave a Reply

Your email address will not be published. Required fields are marked *