WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Conditional tags (is_home) not working in sidebar (4 posts)

  1. Fliepke
    Member
    Posted 6 years ago #

    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

    <?php
    // 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; ?>
        </li>
        </ul>
    
                  <?php if (is_home()){
        echo '<ul><li><h3>Tagcloud</h3>';
     	st_tag_cloud();
    	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 ;) )

  2. SimonJ
    Member
    Posted 6 years ago #

    Try to place :

    <?php wp_reset_query(); ?>

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

    S.

  3. Fliepke
    Member
    Posted 6 years ago #

    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 ;)

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic