Skip to content

Instantly share code, notes, and snippets.

@alexx855
Created May 21, 2017 19:43
Show Gist options
  • Save alexx855/ce3d4a402d57df15ac409f118c606485 to your computer and use it in GitHub Desktop.
Save alexx855/ce3d4a402d57df15ac409f118c606485 to your computer and use it in GitHub Desktop.
Wordpress parent template inherit
<?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');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment