WordPress has a great feature of Page Templates which enable certain pages to use a different template other than the default page.php template.

Using Parent Page Template For Sub PagesSometimes you might require to structure certain content in to pages and sub pages and would like the sub pages to use the parent page’s template. In order to achieve this, you would have to manually select the custom page template while creating each of the sub pages. This is too tidous and manual if you have so many sub pages, I therefore created a custom code snippet in the theme’s functions.php file to automatically use the parent page’s custom page template without having to manually selecting the custom page template when creating the sub pages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
function switch_page_template() {
    global $post;
    // Checks if current post type is a page, rather than a post
	if (is_page())
	{	
		// Checks if page is parent, if yes, return
		if ($post->post_parent == 0)
			return true;
		else if ($post->post_parent != $post->ID)
		{
			$parent_page_template = get_post_meta($post->post_parent,'_wp_page_template',true);
 
			$template = TEMPLATEPATH . "/{$parent_page_template}";
			if (file_exists($template)) {
				load_template($template);
				exit;
			}
		}
	}
}
 
add_action('template_redirect','switch_page_template');

After inserting the above code snippet in functions.php, I created a page, applied the custom template to it and therefore all the sub pages under this page used its parent page template.

Hope it will be useful to someone out there and feel free to contact me in case of any other help or leave a comment below

10 Comments

  • Very useful. Been looking for such a snippet for a very long time.

  • Great snippet!

    a little work around, and it becomes much better! 🙂

    function switch_page_template() {
    global $post;
    // Checks if current post type is a page, rather than a post
    if (is_page()){
    $ancestors = $post->ancestors;

    if ($ancestors) {
    $parent_page_template = get_post_meta(end($ancestors),'_wp_page_template',true);
    $template = TEMPLATEPATH . "/{$parent_page_template}";
    if (file_exists($template)) {
    load_template($template);
    exit;
    }
    } else {
    return true;
    }
    }
    }

    add_action('template_redirect','switch_page_template');

    Now this supports sub sub pages… and sub sub sub pages and so on 🙂

    • Thanks Gleb for the modification

  • I took Gleb’s version and modified it a bit more. Now it will also update the child page’s post meta settings to reflect the new template, which will output the correct body classes etc.

    Also, on the recommendation of someone smarter than me, I removed `global $post` and used `get_the_id()` in place of $post->ID.

  • Just what I was looking for! Thank you!

  • Thanks so much! This was a great help as I had to style content that was already in wordpress’ database and included many subpages.

  • Hi, thanks a lot for the code. Template post_meta search dont work form me, so i change it a little and force it to use slug-specific page template, maybe too strict, but someone can need to use the same work around.

    function switch_page_template() {
    global $post;
    // Checks if current post type is a page, rather than a post
    if (is_page()){
    $ancestors = $post->ancestors;

    if ($ancestors) {
    $parent = get_post(end($ancestors));
    $slug = $parent->post_name;
    $template = TEMPLATEPATH.”/page-“.$slug.”.php”;
    if (file_exists($template)) {
    load_template($template);
    exit;
    }
    } else {
    return true;
    }
    }
    }

  • […] found this code snippet here, but it did not solve my […]

  • The approach above works when the template is stored as post_meta on the post entry. However, if someone used the page-{slug}.php approach to creating the template and it’s not registered as meta, this wouldn’t yield results.

    From Gleb’s modification, i’ve adjusted the code for those who are using the page-{slug}.php approach to this

    function switch_page_template() {
    global $post;

    if ( is_page() ) :

    $ancestors = $post->ancestors;

    if ( $ancestors ) :

    $slug = basename( get_permalink( end( $ancestors ) ) );

    $template = TEMPLATEPATH . ‘/page-‘ . $slug . ‘.php’;

    if ( file_exists( $template ) ) :

    load_template( $template );

    exit;

    endif;

    else :

    return true;

    endif;

    endif;
    }

    add_action( ‘template_redirect’, ‘switch_page_template’ );

Got a comment, Please Say It Out!!