Skip to content

Instantly share code, notes, and snippets.

@claudiosanches
Created August 16, 2012 16:53
Show Gist options
  • Save claudiosanches/3371633 to your computer and use it in GitHub Desktop.
Save claudiosanches/3371633 to your computer and use it in GitHub Desktop.
Manipulate Child Pages to Use Parent Page Templates Automatically
<?php
// Manipulate Child Pages to Use Parent Page Templates Automatically
function switch_page_template() {
global $post;
// Checks if current post type is a page, rather than a post
if (is_page()) {
$current_page_template = get_post_meta($post->ID, '_wp_page_template', true);
if (!$current_page_template) {
$parent_page_template = get_post_meta($post->post_parent, '_wp_page_template', true);
$parents = (is_page() && $post->post_parent == $post->ID) ? true : false;
if ($parents == true) {
update_post_meta($post->ID, '_wp_page_template', $parent_page_template, $current_page_template);
}
}
}
}
add_action('save_post', 'switch_page_template');
add_action('the_post', 'switch_page_template');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment