Check Template of Parent Page

I’ve always wanted to apply automatic inherited styles to pages that are child pages of a custom template, and now I can!

function is_page_template_deep( $template = '' ) { // place template name in 
	if ( !empty( $template ) && is_page() ) {
		$parents  = get_post_ancestors( get_the_ID() );
		$id       = ( $parents ) ? $parents[count($parents)-1]: get_the_ID();
		if ( !empty( $id ) && get_page_template_slug( $id ) == $template ) {
			return $id;
		}
	}
	return false;
}

Handy function found here.

Slight modification to show the content on the child page only:

function is_page_template_deep( $template = '' ) {
	if ( !empty( $template ) && is_page() ) {
		global $post;
		
		$parents  = get_post_ancestors( get_the_ID() );
		$id       = ( $parents ) ? $parents[count($parents)-1]: get_the_ID();
		
		if($post->post_parent) {
			if ( !empty( $id ) && get_page_template_slug( $id ) == $template ) {
				return $id;
			}
		}
		
	}
	return false;
}

Call in template:

is_page_template_deep('template-name.php');

Leave a Reply

Posted in php
katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.