WordPress.org

Ready to get started?Download WordPress

Forums

is_tree() function (7 posts)

  1. syncbox
    Member
    Posted 4 years ago #

    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 (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

  2. tomontoast
    Member
    Posted 4 years ago #

    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)?

  3. syncbox
    Member
    Posted 4 years ago #

    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.

  4. syncbox
    Member
    Posted 4 years ago #

    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.

  5. Clicknathan
    Member
    Posted 4 years ago #

    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!!!

  6. fele
    Member
    Posted 3 years ago #

    Very usefull. I use the Snippet 4 but it would better if we could use array() as is_page() and is_single(). Any ideas?

  7. inathought
    Member
    Posted 3 years ago #

    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;
    	}
    };

Topic Closed

This topic has been closed to new replies.

About this Topic