Created
May 21, 2017 19:43
-
-
Save alexx855/ce3d4a402d57df15ac409f118c606485 to your computer and use it in GitHub Desktop.
Wordpress parent template inherit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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