badcrocodile
2/13/2015 - 10:54 PM

Detect if page is parent or ancestor of parent

Detect if page is parent or ancestor of parent

<?php
/* Sourced from https://wordpress.org/support/topic/check-if-current-page-is-child-or-grandchild-of-a-page */
// If is page ($pid) or child of $pid
function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
	global $post;         // load details about this page
	$anc = get_post_ancestors( $post->ID );
	foreach($anc as $ancestor) {
		if(is_page() && $ancestor == $pid) {
			return true;
		}
	}
	if(is_page()&&(is_page($pid)))
		return true;   // we're at the page or at a sub page
	else
		return false;  // we're elsewhere
}
?>