Support » Themes and Templates » Repeating Content – query_posts problem

  • Resolved Lindsey



    I’ve stared at this for the last hour and can’t figure out what the problem is, so maybe a pair of fresh eyes will be able to spot what is wrong.

    So, I’m using query_posts to call different numbers of posts from different categories on my pages. This has worked without any problems so far. For example on my index page I have two content boxes which display certain content from 2 seperate categories. Than in my sidebar I have used it to display the last three posts from another category.

    So, what I want to do next is use it for my navigation to control the list of title links to the posts. So this is the code I have used:

    <ul id="nav">
    	<li><a href="<?php echo get_option('home'); ?>" class="home">Home</a></li>
        <li><a href="/about" class="about">About</a></li>
        <li><a href="/services" class="services">Services</a></li>
        <li><a href="#" class="athletes">Athletes</a>
    		<?php query_posts('showposts=20&cat=5'); ?>
            <?php while (have_posts()) : the_post(); ?>
    			<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></li>
             <?php endwhile; ?>
    	<li><a href="/timetables" class="timetables">TimeTables</a></li>
        <li><a href="/blog" class="blog">Blog</a></li>
        <li><a href="#" class="contact">Contact</a></li>

    But, when I place this code in my header.php file, and upload it to the site all the pages and posts point to the last post in category id #5. For some reason I guess the loop isn’t ending for that section, but I’m not sure why? Is it because it’s in my header.php file? When I remove the code from my header.php file everything returns to normal.

    Suggestions, ideas, solutions?


Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Samuel Wood (Otto)

    (@otto42) Admin

    When you do a query_posts(), you overwrite the old query. In other words, there’s one active query at a time, and you’ve overriden it with your query.

    You need to reset the query before your main Loop. Try adding this just before your main Loop:

    Ah great. That worked. Thanks so much. I knew it would be something simple that I overlooked.

    Otto is a rock star 🙂



    Don’t forget to make sure that $query_string is accessible with global $query_string; before query_posts($query_string);

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Repeating Content – query_posts problem’ is closed to new replies.