Set page template automatically based on parent

On the admin side, you could update the meta data for the page-post_type programmatically:

global $post;
if ( 
    'swiss_cheese' === $post->post_parent 
    AND is_admin()
)
    update_post_meta( $post->ID, '_wp_page_template', 'some_template.php' );

On the visitor facing side, you can simply jump into template redirect:

function wpse63267_template_include( $template )
{
    global $post;

    if ( 'swiss_cheese' === $post->post_parent )
    {
        $new_template = locate_template( array( 'swiss-cheese-template.php' ) );
        if ( ! empty( $new_template ) ) {
            return $new_template ;
        }
    }
    return $template;
}
add_filter( 'template_include', 'wpse63267_template_include', 99 );

Leave a Comment