Support » Themes and Templates » Conditional tags (is_home) not working in sidebar

  • Resolved Fliepke


    I’m working on my own blog and I want to display a tagcloud in my sidebar if the user is on the index/home. This is the code that I’m using that doesnt work

    // this is where the last three headlines are pulled from the News category
    		query_posts('showposts=3&cat=5'); ?>
     	<?php while (have_posts()) : the_post(); ?>
        <?php	$values = get_post_custom_values("Image");
    	// this checks to see if an image file exists
    	if (isset($values[0])) {
          <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo get_option('home'); ?>/wp-content/uploads/<?php $values = get_post_custom_values("Image"); echo $values[0]; ?>" height="55px" width="275px" alt="" /></a>
          <?php } ?>
     	<?php endwhile; ?>
                  <?php if (is_home()){
        echo '<ul><li><h3>Tagcloud</h3>';
    	echo '</li></ul>';
     } ?>

    The funny thing is, this doesn’t work. If I place the Tagcloud code before the “query_posts” it works just fine. Something breaks because of query_posts and I just don’t get it. Any help would be appreciated.
    (I customized the mimbo2 template for my own blog, thats where the majority of the code comes from 😉 )

Viewing 3 replies - 1 through 3 (of 3 total)
  • Try to place :

    <?php wp_reset_query(); ?>

    After the <?php endwhile; ?> of your query_posts loop, or just before your conditional tag code…


    Thanks, seems to work!
    Don’t have a clue why, but this will do.
    If anyone could explain it to me, that would be great 😉

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    When you use query_posts, you’re changing the main query for the page. This changes the results of all the is_whatever statements, which are all based on that main query.

    Fortunately, the original unmodified query is always stored. wp_reset_query() brings that query back to the front, undo’ing any changes you made with query_posts.

    Generally speaking, you should not use query_posts except to change the main query that displays the posts on the page. For displaying posts elsewhere, you should create a new WP_Query and use that instead. Then you would not have this issue.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Conditional tags (is_home) not working in sidebar’ is closed to new replies.