• Gilad

    (@virtualgilad)


    Greetings, all!

    I need to build a page that has three loops running on it:

    1. Each loop only displays posts from one category.
    2. Each loop only displays the four most recent posts from that category.
    3. No post will ever appear in more than one of these categories (enforced, for now, by editorial policy/practice rather than programmatically).
    4. The loops will always appear in the same order.
    5. There are no additional versions/implementations of this page that will vary the above patterns or quantities.

    I’ve already looked into various possibilities (wp_query, query_posts, etc), but each of the examples I’ve found runs me into a wall (forcing a loop that cannot narrow down to one category, building a loop that depends upon a counter that is invalidated by [if (in_category(x) continue], etc).

    So before I embark on an open ended experiment with every example out there (to say nothing about my concerns regarding an experiment that appears to work but comes back to bite me later), I’d like to ask here:

    • Does anyone here have any advice on how to pursue this?
    • Does anyone here have any working examples that are known to have survived ongoing use?

    TIA!
    Gilad

Viewing 3 replies - 1 through 3 (of 3 total)
  • I don’t have 3 loops showing 4 posts, but I have 4 loops showing 1 post each here.

    All 4 loops look practically identical to this one, except for the category name and the ‘not found’ message:

    <!-- Fourth Loop for Down the Road  -->
    <?php $more = 0;
    query_posts('category_name=DownTheRoad&posts_per_page=1');
    if (have_posts()) {
       <?php while (have_posts()) : the_post(); ?>
          <div class="post" id="post-<?php the_ID(); ?>">
             <h4><?php the_title(); ?></h4>
             <div class="entry">
                <?php the_content(); ?>
             </div>
          </div>
       <?php endwhile;
    } else {
       echo "Sorry, no long term plans today.<br /><br /><br />"; ?>
    } ?>

    Before the first loop, I saved the original query with this:

    <?php $temp_query = $wp_query; ?>

    And, after the last loop, I restored it:

    <?php $wp_query = $temp_query; ?>

    Thread Starter Gilad

    (@virtualgilad)

    Very interesting. Thank you, vtxyzzy!

    I was under the impression that query_posts should not normally be called repeatedly within a single page. Am I wrong? Or are you doing something here that overcomes the risks?

    Regarding your last two samples:

    1. Could you explain the significance of saving the original query (especially in doing so only once, and before the FIRST loop)?
    2. Could you explain the significance of restoring the query after the last loop?

    Thanks again for your input!
    Gilad

    I was under the impression that query_posts should not normally be called repeatedly within a single page. Am I wrong? Or are you doing something here that overcomes the risks?

    That is the purpose of saving the query before the first loop – save the query before my first special query steps on it.

    After the last loop, restore the original query in case any plugins depend on it.

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

The topic ‘Three Loops, Please…’ is closed to new replies.