WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to stop query_posts ? (6 posts)

  1. SimonJ
    Member
    Posted 6 years ago #

    OK... here is a problem that I never solved.

    If I have a "classic loop", just like in single.php for instance, folowing a loop based on query_posts, the query seems to saty active for the second loop...

    Let's say I want to use this code in my header :

    <?php query_posts('cat=12&showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    --- My stuff ---
    <?php endwhile; ?>

    And, this loop in my single.php :

    <?php while (have_posts()) : the_post(); ?>
    --- My stuff ---
    <?php endwhile; ?>

    Since I called the query_posts in the header before the loop to display the content of the single posts, the query_posts, and the arguments it contains, stays active...

    So I end up with a single posts whom display, in this code example, one post from category 12...

    Another example, if I want to use tables to display some stuff (and please, don't tell me I shouldn't use table...:-))

    If I call the query_posts in the first column (td), then the "classic loop" for the single posts in the second column, the query_posts stay active.

    I understand why... the second <?php while (have_posts()) : the_post(); ?> is just another call to this same query, so WordPress display the stuff based on the same result...

    So is there a way to stop the query_posts ?

    Thanks!

    S.

  2. Kafkaesqui

    Posted 6 years ago #

    At the end of your query_posts() loop try:

    <?php wp_reset_query(); ?>
  3. SimonJ
    Member
    Posted 6 years ago #

    Woahhh! Wonderful Kaf! Works like a charm!

    Thanks x 1000.

    This tag must be in the query_posts codex documentation!

    Indispensable! :-)

    S.

  4. mo2007
    Member
    Posted 6 years ago #

    am having a similar problem.....I have 3 sections of my website that use the query_posts:

    <?php $posts = query_posts( "cat=8&showposts=1"); ?> <?php if( $posts ) : ?>
    
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>

    what I needed appears on the front page. I also use it for my index.php to exclude categories that I don't want to show, the problem is, how do I create my single.php

    I tried copying everything in index.php and pasting it into single.php, but nothing happens. I have put <?php wp_reset_query(); ?> at the end of all the 3 sections....nothing :(

    any helps would be appreciated..thanx in advance

  5. moshu
    Member
    Posted 6 years ago #

    What do you mean how to create the single.php? Obviously it cannot have 3 loops, only one - that's why it is "single".

  6. mo2007
    Member
    Posted 6 years ago #

    you see the 3 loops are in the header.php so it is seperated...which makes the single.php file have only one query_post

Topic Closed

This topic has been closed to new replies.

About this Topic