Skip to content

Instantly share code, notes, and snippets.

@brycejacobson
Created August 13, 2013 20:54
Show Gist options
  • Save brycejacobson/6225573 to your computer and use it in GitHub Desktop.
Save brycejacobson/6225573 to your computer and use it in GitHub Desktop.
WordPress is_tree function to see if a page is an ancestor.
<?php
//* Do NOT include the opening php tag
//* Determin if a page is an ancestor
function is_tree($pid) // $pid = The ID of the page we're looking for pages underneath
{
global $post; // load details about this page
$ancestors = get_post_ancestors($post->$pid);
$root = count($ancestors) - 1;
$parent = $ancestors[$root];
if(is_page() && (is_page($pid) || $post->post_parent == $pid || in_array($pid, $ancestors)))
{
return true; // we're at the page or at a sub page
}
else
{
return false; // we're elsewhere
}
};
<?php
//* Do NOT include the opening php tag
if (is_tree(2)) // 2 is the id of the page we are looking under
{
// stuff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment