WordPress.org

Support

Support » Themes and Templates » Conditional Tags not working with multiple loops

Conditional Tags not working with multiple loops

  • I have a sidebar with several loops in it. Each loop looks something like this:

    $temp = $the_query;
    		$the_query= null;
    		$the_query = new WP_Query;
    		$the_query->query('cat=54&showposts=1');
    		while ($the_query->have_posts()) : $the_query->the_post(); ?>
            	<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <?php the_content(); ?>
    
            <?php endwhile; ?>
            <?php $the_query  = null; $the_query  = $temp; ?>

    The problem is, I need to be able to hide one of these loops if I’m on a single post in the category ’54’. So I added this:

    <?php if (!( in_category('54') )) { ?>
    
    ... query loop ...
    
    <?php } ?>

    But it makes no change to what is outputted. Whether the post is in that category or not, it shows up.

Viewing 5 replies - 1 through 5 (of 5 total)
  • esmi

    @esmi

    Forum Moderator

    Try:

    <?php if (is_single() && !in_category('54') ) { ?>
    
    ... query loop ...
    
    <?php } ?>

    still there :/ If I remove all of the other loops from the sidebar, it works. Is it maybe because I’m using the same “$the_query” for every loops?

    Moderator Michael

    @alchymyth

    Forum Moderator

    my guess is that i would try and save the results of ‘is_single()’ and ‘in_category()’ into a variable immedeately before the first loop – assuming that the original query results (?) are still present (can’t describe it any better).
    and then use these variables to check against for the one loop.

    esmi

    @esmi

    Forum Moderator

    esmi, that worked great, thanks! any known caveats of using that?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Conditional Tags not working with multiple loops’ is closed to new replies.