Skip to content

Instantly share code, notes, and snippets.

@kisabelle
Created January 22, 2014 19:04
Show Gist options
  • Save kisabelle/8565154 to your computer and use it in GitHub Desktop.
Save kisabelle/8565154 to your computer and use it in GitHub Desktop.
WordPress: Automatically Apply Parent Page Template to Child Pages by Matovu Richard
<?php
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');
/* Source: http://www.matrich.net/blog/wordpress/how-to-automatically-apply-parent-page-template-to-all-sub-pages-in-wordpress.htm */
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment