• Resolved Overflow

    (@acrane)


    Has anyone run into the problem with the is_tree function that having an else will not work? If I delete the last else statement so that it cannot take on a last variable, it works as expected.

    if (is_tree(33)) {
     	$logoClass = 'logo-yellow ';
    	$nav = "conway"; 
    
    } elseif (is_tree(35)) {
    	$logoClass = 'logo-red ';
    
    } else { // if is something else
    	$logoClass = 'logo-blue';
    }

    I am using both
    <?php wp_reset_query(); ?>
    <?php wp_reset_postdata(); ?>

    before the call…. trying to make sure a query isn’t interfering.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    is_tree() will only work in the “Loop”. Don’t reset a query unless you started a different one to begin with, doing so will cause loop data to get out of sync. A new query must be matched with a reset and vice versa. And never call both together unless each is matched to it’s related query method.

    If the global $post object that contains the current post data inside the loop is invalid, is_tree() will always return false even though it should have returned true if $post was in order, leading to strange behavior in if/elseif/else structures.

    Thread Starter Overflow

    (@acrane)

    Ok, good points.

    So When I do complex templates with multiple queries and then I get down to the footer and want to check which page i’m on (to show an appropriate logo), how should I reset the query, and get the postdata correct so that is_tree works as expected?

    Moderator bcworkz

    (@bcworkz)

    If you are still in the main loop and the other queries have been properly reset when they are finished, in_tree() should just work.

    If you are out of the loop, you aren’t supposed to use in_tree(). You should be able to still get the current page with get_queried_object(). You can try to set the global $post just like the_post() would to fool in_tree() into believing it is in the loop. Or you could access the properties in the queried object in the same manner that in_tree() does to make the determination if the object is in the branch of the post ID passed to in_tree(). In other words, create an outside of the loop version of in_tree().

    Or move in_tree() into the loop and save it’s determination in a variable for later use outside of the loop.

    Thread Starter Overflow

    (@acrane)

    Thanks for the ideas bcworkz. I’ve found it does work out of the loop. By reseting the postdata, the global postdata of what page you’re on is able to be retrieved with get_queried_object(); and the conditional function that I’m using works.

    I ended up in the end having a silly mistake, I had a if, in the middle of elseif’s and it was throwing the function off.

    Thanks for the response.

    ps. the original function i pasted here, did not have the problem I had, i had simplified it for posting. (bad idea)

    Moderator bcworkz

    (@bcworkz)

    Those silly mistakes seem to get me all the time! I’m glad you worked it out.

    By way of explanation of why is_tree() works outside the loop in your situation, it’s because you are only using it on single pages. is_tree() relies on the global $post to determine the current post in the loop. When you properly reset other queries, the original content of $post is restored. This data persists even outside the loop, but it is essentially “stale” data, left over from when the loop completed. It’s not the best practice to use stale data, but for single pages at least, it obviously works.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘is_tree not working as expected’ is closed to new replies.