Improved is_tree function. If page is an ancestor. Child, Grandchild…

This site has a function to test if a page is a child or not. Problem is that it only goes one level deep. Use this to go further.

This goes in the functions file.

function is_tree($pid)
{
  global $post;

  $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;
  }
  else
  {
    return false;
  }
};

Usage:

if (is_tree(2)) {
   // stuff
}

Leave a Reply

Your email address will not be published. Required fields are marked *

40 − = 38