WordPress.org

Ready to get started?Download WordPress

Forums

Does WordPress 3.5 Still Support is_tree ? (15 posts)

  1. jmarcello
    Member
    Posted 1 year ago #

    Greetings:

    I have been adding is_tree to my themes with this code and then using is_tree to conditionally display or not display certain widgets in my side bar:

    function is_tree($pid) {
        global $post;
        $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;
        	else
        	return false;
    };

    Today upon upgrade to 3.5 I had several client's sites blow up. Does anyone else use is_tree as a conditional and is this method still functioning in 3.5. For me it does not seem to work any more

    If this is not the preferred method, what other solution is there for targeting parent/child pages?

    Any help would be greatly appreciated...

    Best Regards!

  2. is_tree isn't a part of WP, so I don't think that it would matter.

    I'd suspect the problem is get_post_ancestors - http://core.trac.wordpress.org/changeset/21952

  3. jmarcello
    Member
    Posted 1 year ago #

    So would you know how to modify the above function so that it works with WordPress 3.5 so that I may continue accessing is_tree?

    I have this deployed on too many sites to count and would be very appreciative for a fix...

  4. Dion Hulse
    WordPress Dev
    Posted 1 year ago #

    @jmarcello What's the actual error message you're getting from the function at present?

  5. jmarcello
    Member
    Posted 1 year ago #

    Warning: Invalid argument supplied for foreach() in /home/tsg020/public_html/wp-content/themes/vvagroup/custom/custom_functions.php on line 80

    function is_tree($pid) {
        global $post;
        $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;
        	else
        	return false;
    };

    The foreach statement is line 80

  6. Dion Hulse
    WordPress Dev
    Posted 1 year ago #

    The only way you can get that error message, is if the $post global is not yet set up.. That shouldn't have changed in 3.5..

    Using the exact same function, worked flawlessly for me in TwentyTwelve, I did however take the liberty of cleaning it up a little bit after testing it:

    function is_tree($pid) {
            global $post;
            if ( ! is_page() )
                    return false;
            if ( is_page( $pid ) )
                    return true;
            $anc = get_post_ancestors( $post );
            if ( in_array( $pid, $anc ) )
                    return true;
            return false;
    }
  7. Dion Hulse
    WordPress Dev
    Posted 1 year ago #

    If you would like me to take a closer look at it and see if I can reproduce what you're seeing, you can email me a theme which reproduces the error (but works under 3.4) to [email removed]

  8. jmarcello
    Member
    Posted 1 year ago #

    Dion...

    That seemed to fix it. Thank you ever so much.

    Regards,
    John

  9. Dion Hulse
    WordPress Dev
    Posted 1 year ago #

    @jmarcello: I'd still be interested in seeing how your previous usage had broken, because for all intents and purposes, it shouldn't have happened.
    The offer to look through a theme and work out the issue still stands.

  10. jmarcello
    Member
    Posted 1 year ago #

    The configuration on this one is a little complex, it is set up as a Child Theme of Thesis 1.8.2. And then the is_tree call was being used in the Widget Logic plugin to turn on and off side bar widgets.

    I would gladly send you the custom_functions.php and custm.css file which would give you enough to reproduce the Thesis Child theme if you like...

  11. Dion Hulse
    WordPress Dev
    Posted 1 year ago #

    Thanks, I was able to duplicate it using Widget logic.

    Widget logic runs the conditional check quite a few times for each widget, some of those times it runs before WordPress has loaded the posts, which is why the $post global wasn't yet set (in both 3.4 and 3.5). 3.5 now returns false instead of an empty array in cases like this, causing the warning.

    However, using the same code under 3.4 with PHP 5.4 generates PHP Warnings as well, meaning under older PHP versions it was silently failing, but it's still a change in behaviour.

    I've opened http://core.trac.wordpress.org/ticket/22882 to track the issue.

  12. jmarcello
    Member
    Posted 1 year ago #

    Thanks you for your hard work, expertise and dedication...

  13. iftomkins
    Member
    Posted 1 year ago #

    Hi Dion and Marcello!

    I had the exact same issue upon upgrading to 3.5, but replacing my old function with Dion's cleaned up function (above) did the trick.

    Thank you both very much!

  14. rlinn
    Member
    Posted 1 year ago #

    Thanks Dion,
    Also using Widget Logic and is_tree(), and your code fixed the same problem for me. The old is_tree() is Snippet 4 of the documentation on Conditional Tags. This will need an update.

  15. adambundy
    Member
    Posted 1 year ago #

    @Dion, I was having an issue with a custom body_class addition in a project I inherited, and it had failed since upgrade to 3.5 as well. After reading this thread, I suspected that the root might be this same issue. Note to the dev team that this function was working before upgrade to 3.5 without the global $post declaration, just the commented out $typeNow. After I added the $post declaration, it works. Could you explain the reason and how I/we can avoid similar problems in other code?

    function highest_parent_body_class($classes = '') {
    	//global $typeNow;
    	global $post, $typenow;
    
    	$anc = get_post_ancestors($post->ID);
    
    	if (count($anc)) {
    		$highestParentID = end($anc);
    		$highestParent = get_post($highestParentID);
    		$classes[] = 'desc-'.$highestParent->post_name;
    		array_pop($anc);
    		$secondHighestParentID = end($anc);
    		$secondHighestParent = get_post($secondHighestParentID);
    		$classes[] = 'desc2-'.$secondHighestParent->post_name;
    	} else {
    		$thisOne = get_post($post->ID);
    		$classes[] = 'root root-'.$thisOne->post_name;
    	}
    
    	return $classes;
    }
    add_filter('body_class','highest_parent_body_class');

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.