• Resolved mdp8593

    (@mdp8593)


    I’m creating a category page that will display posts 3 different ways. All the posts are in the same category; it’s just that I want them displayed as follows:

    Post Listing 1 (1 Post) – Feature Article for That Category.

    Post Listing 2 (Next 6 Posts) – Past Feature Articles.

    Post Listing 3 (Next 13 Posts) – Other posts in the category.

    All three post listings will be displayed differently, and I have no problem with that part of it.

    The problem is how do I prevent the posts from repeating in each of the post listings.

    So far, this is what I have for code:

    <div class="catfeature">
    <?php
    $categoryvariable=$cat;
    $my_query = new WP_Query('cat=' . $categoryvariable. '&showposts=1');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;
    ?>
    <!-- do stuff ... -->
    <?php endwhile; ?>
    </div>
    <!-- End Cat Feature ... -->
    <div class="catrecentfeatures">
    <h2>Recent Features</h2>
    <?php
    $categoryvariable=$cat;
    $my_query = new WP_Query('cat=' . $categoryvariable. '&showposts=6');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;
    ?>
    <!-- do stuff ... -->
    <?php endwhile; ?>
    </div>
    <!-- End Recent Features ... -->
    <div class="catmorefeatures">
    <h2>More Features</h2>
    <?php
    $categoryvariable=$cat;
    $my_query = new WP_Query('cat=' . $categoryvariable. '&showposts=-1');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;
    ?>
    <!-- do stuff ... -->
    <?php endwhile; ?>
    </div>
    <!-- End More Features ... -->

    I realize I’m starting a new query each time, and I know that’s part of the problem. I tried to hack the code found on http://codex.ww.wp.xz.cn/The_Loop#Multiple_Loops_in_Action,
    but that only covers 2 loops, and I still get a repeat in the 3rd loop.

    Any help would be much appreciated.

    Best – Michael

Viewing 6 replies - 1 through 6 (of 6 total)
  • There’s no way to offset posts (which is what you’re asking for) in the various queries using query_posts() or creating a new wp_query object.

    One way to do what you’re after is to use get_posts(), as it offers an ‘offset’ parameter.

    Another perhaps better option is to set off a single query, then test on an increment variable of your own making. Even better, this can be done within the default Loop. Example:

    <?php while(have_posts()) : the_post(); $postcount++; ?>

    <?php if( $postcount == 1 ) : // feature article ?>

    ~ Feature article layout ~

    <?php elseif( $postcount > 1 && $postcount < 8 ) : // recent features ?>

    ~ Recent features layout ~

    <?php else : // more features ?>

    ~ More features layout ~

    <?php endif; ?>

    <?php endwhile; // end loop ?>

    ——————–

    As a further example, if the only change to layout in these three “loops” is your <div> and <h2> elements, you could do this:

    <?php while(have_posts()) : the_post(); $postcount++; ?>

    <?php if( $postcount == 1 ) : // feature article start ?>
    <div class="catfeature">
    <?php elseif( $postcount == 2 ) : // recent features start ?>
    </div>
    <!-- End Cat Feature ... -->
    <div class="catrecentfeatures">
    <h2>Recent Features</h2>
    <?php elseif( $postcount == 8 ) : // more features start ?>
    </div>
    <!-- End Recent Features ... -->
    <div class="catmorefeatures">
    <h2>More Features</h2>
    <?php endif; ?>

    <!-- do stuff ... -->

    <?php endwhile; // end loop ?>
    </div>
    <!-- End More Features ... -->

    Thread Starter mdp8593

    (@mdp8593)

    Great Kaf.

    Works perfectly. Thanks!

    kaf takes a percentage of whatever your client is paying you for his work.

    Thread Starter mdp8593

    (@mdp8593)

    and i’d gladly pay him.

    Thread Starter mdp8593

    (@mdp8593)

    I had some trouble with the code provided by Kaf, so I played around with it, and here’s what I got to work:

    <div class="catfeature">
    <?php $categoryvariable=$cat;
    $my_query = new WP_Query('cat=' . $categoryvariable. '&showposts=1');
    while ($my_query->have_posts()) : $my_query->the_post();
    ?>
    <!-- do stuff ... -->
    <?php endwhile; ?>
    </div>
    <div class="catrecentfeatures">
    <h2>Recent Features111</h2>
    <?php $categoryvariable=$cat;
    $my_query = new WP_Query('cat=' . $categoryvariable. '&showposts=2&offset=1');
    while ($my_query->have_posts()) : $my_query->the_post();
    ?>
    <!-- do stuff ... -->
    <?php endwhile; ?>
    </div>
    <div class="catmorefeatures">
    <h2>More Features</h2>
    <?php while(have_posts()) : the_post(); $postcount++; ?>
    <?php if( $postcount > 3 ) : // feature article ?>
    <!-- do stuff ... -->
    </div>
    <?php endif; ?>
    <?php endwhile; // end loop ?>
    </div>

    It produces a category page that looks roughly like this:
    http://groomingfile.com/category/style

    I had some trouble with the code provided by Kaf

    I didn’t run it through the grinder or anything, but I did test it before posting, and saw no apparent issues. What was the trouble you had with it?

    A couple things to note on your code:

    1. There’s no need to assign $cat to $categoryvariable and use the latter in your query. You’re not breaking anything, but it’s an unnecessary extra step.

    2. Well well, the ‘offset’ argument works in query_posts() / WP_Query(). Hey, who knew? Looks like it was added in June or so (i.e. the last few 2.0.x iterations have it).

    I’ll try not to harp on the issue of the use of multiple queries here…

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘How to do 3 Loops Without Repeating Posts???’ is closed to new replies.