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

Viewing 14 replies - 1 through 14 (of 14 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    πŸ³οΈβ€πŸŒˆ Advisor and Activist

    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

    Thread Starter jmarcello

    (@jmarcello)

    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…

    Dion Hulse

    (@dd32)

    Meta Developer

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

    Thread Starter jmarcello

    (@jmarcello)

    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

    Dion Hulse

    (@dd32)

    Meta Developer

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

    (@dd32)

    Meta Developer

    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]

    Thread Starter jmarcello

    (@jmarcello)

    Dion…

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

    Regards,
    John

    Dion Hulse

    (@dd32)

    Meta Developer

    @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.

    Thread Starter jmarcello

    (@jmarcello)

    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…

    Dion Hulse

    (@dd32)

    Meta Developer

    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.

    Thread Starter jmarcello

    (@jmarcello)

    Thanks you for your hard work, expertise and dedication…

    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!

    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.

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

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Does WordPress 3.5 Still Support is_tree ?’ is closed to new replies.