Skip to content

Instantly share code, notes, and snippets.

@isGabe
Created September 6, 2013 18:05
Show Gist options
  • Save isGabe/6467584 to your computer and use it in GitHub Desktop.
Save isGabe/6467584 to your computer and use it in GitHub Desktop.
WordPress - Automatically apply a page template to children of a page #WordPress #snippet
/**
* Automatically apply parent page template
*
* Inspired by: http://is.gd/wp_apply_parent_page_template
*/
function fstop_use_parent_page_template() {
// Checks if current post type is a page, rather than a post
if ( is_page() ){
$ancestors = get_post_ancestors( get_the_id() );
if ( $ancestors ) {
$parent_page_template = get_post_meta( end( $ancestors ), '_wp_page_template', true );
$template = TEMPLATEPATH . "/{$parent_page_template}";
$current_page_template = get_post_meta( get_the_id(),'_wp_page_template', true );
if ( file_exists( $template ) ) {
load_template( $template );
update_post_meta( get_the_id(), '_wp_page_template', $parent_page_template, $current_page_template );
exit;
}
} else {
return true;
}
}
}
add_action( 'template_redirect', 'fstop_use_parent_page_template' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment