WordPress.org

Ready to get started?Download WordPress

Forums

Conditional Tags not working with multiple loops (6 posts)

  1. Tammy Hart
    Member
    Posted 4 years ago #

    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.

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    Try:

    <?php if (is_single() && !in_category('54') ) { ?>
    
    ... query loop ...
    
    <?php } ?>
  3. Tammy Hart
    Member
    Posted 4 years ago #

    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?

  4. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    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.

  5. esmi
    Forum Moderator
    Posted 4 years ago #

  6. Tammy Hart
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.