WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Repeating Content - query_posts problem (5 posts)

  1. CSSgirl
    Member
    Posted 6 years ago #

    Hello,

    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>
    		<ul>
    		<?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; ?>
    		</ul>
    	</li>
    	<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>
    </ul>

    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?

    Thanks!
    Lindsey

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

    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:
    query_posts($query_string);

  3. CSSgirl
    Member
    Posted 6 years ago #

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

  4. Ivovic
    Member
    Posted 6 years ago #

    Otto is a rock star :)

  5. kurisukun
    Member
    Posted 6 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic