Title: is_tree() function
Last modified: August 19, 2016

---

# is_tree() function

 *  [syncbox](https://wordpress.org/support/users/syncbox/)
 * (@syncbox)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/is_tree-function/)
 * Why isn’t the conditional function is_tree() a CORE part of WordPress? Why do
   I have to add it to use it?
 * If you’ve EVER struggled over trying to make dynamic sidebars or includes or 
   any other changes that should propagate to a page and it’s children, check out
   is_tree(); IT ROCKS and it SHOULD be built into the program by default, imo.
 * [http://codex.wordpress.org/Conditional_Tags](http://codex.wordpress.org/Conditional_Tags)(
   scroll down to snippet4)
 * Here’s the function, but you have to add it to use it per instructions on the
   Conditionals Tags page:
 *     ```
       function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
       	global $post;         // load details about this page
       	if(is_page()&&($post->post_parent==$pid||is_page($pid)))
                      return true;   // we're at the page or at a sub page
       	else
                      return false;  // we're elsewhere
       };
       ```
   
 * Vote here: [http://wordpress.org/extend/ideas/topic.php?id=3139](http://wordpress.org/extend/ideas/topic.php?id=3139)

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [tomontoast](https://wordpress.org/support/users/tomontoast/)
 * (@tomontoast)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/is_tree-function/#post-1204592)
 *     ```
       function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
       	global $post;         // load details about this page
       	if(is_page()&&($post->post_parent==$pid||is_page($pid)))
                      return true;   // we're at the page or at a sub page
       	else
                      return false;  // we're elsewhere
       };
       ```
   
 * What does this code do exactly, it checks if your on a page and $pid is the id
   of a page or the current page’s post parent. Don’t get how it could be useful.
   Why not just use !empty($post->post_parent)?
 *  Thread Starter [syncbox](https://wordpress.org/support/users/syncbox/)
 * (@syncbox)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/is_tree-function/#post-1204796)
 * I use it to load custom sidebars or special style rules for sub-pages of a parent…
   or you can use it like the Snippet#3 example, to load a custom header image for
   different pages without having to write arrays or complex if/elseif/else logic.
   the possibilities are numerous and as one who has used WordPress as a CMS for
   years, this function really eased my workload.
 * You can use it in an array of pages… so, rather than use if_page(‘x’) || is_page(‘
   x’) etc etc for all sub-pages of a parent page, you can simply declare the parent
   and apply your logic to its IMMEDIATE child pages without applying it to THEIR
   children.
 * That’s the main thing, for me. If you use something based on child_of=, then 
   it applies to all of them
 * Call me crazy, but this has become a favorite for me.
 *  Thread Starter [syncbox](https://wordpress.org/support/users/syncbox/)
 * (@syncbox)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/is_tree-function/#post-1204797)
 * just to add one more thing… the beauty of this for me is not having to know the
   ID or name or page-slug of any FUTURE sub pages of a parent page.
 * When I build the array of parent pages, I don’t have to know about the children.
 *  [Clicknathan](https://wordpress.org/support/users/clicknathan/)
 * (@clicknathan)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/is_tree-function/#post-1204839)
 * It is fabulous, I use it on nearly every theme I build now. It eliminates the
   need to choose templates every time you create a page, and so much more. Thank
   you!!!
 *  [fele](https://wordpress.org/support/users/fele/)
 * (@fele)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/is_tree-function/#post-1204849)
 * Very usefull. I use the Snippet 4 but it would better if we could use array()
   as is_page() and is_single(). Any ideas?
 *  [inathought](https://wordpress.org/support/users/inathought/)
 * (@inathought)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/is_tree-function/#post-1204856)
 * Great script. I’ve updated it to work with navigation with more than two levels
   deep without losing the absolute parent.
 *     ```
       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;
       	}
       };
       ```
   

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘is_tree() function’ is closed to new replies.

## Tags

 * [child pages](https://wordpress.org/support/topic-tag/child-pages/)
 * [conditional tags](https://wordpress.org/support/topic-tag/conditional-tags/)
 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [pages](https://wordpress.org/support/topic-tag/pages/)

 * In: [Requests and Feedback](https://wordpress.org/support/forum/requests-and-feedback/)
 * 6 replies
 * 5 participants
 * Last reply from: [inathought](https://wordpress.org/support/users/inathought/)
 * Last activity: [15 years, 9 months ago](https://wordpress.org/support/topic/is_tree-function/#post-1204856)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
