Skip to content

Instantly share code, notes, and snippets.

@habibmac
Created June 6, 2012 16:52
Show Gist options
  • Save habibmac/2883235 to your computer and use it in GitHub Desktop.
Save habibmac/2883235 to your computer and use it in GitHub Desktop.
WP: Conditional to check for hierarchy descendant
// A conditional function to check if the current page is a descendant of the ID given to it. Useful for determining if a page is a grandchild, great-grandchild or father down the hierarchy tree.
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment