WordPress.org

Ready to get started?Download WordPress

Forums

Looping the Loop (2 posts)

  1. Root
    Member
    Posted 9 years ago #

    I have got my loop repeating but not in a way that is useful. As I want to run different queries in both loops presumably I need to swap the global variable as per Alexs stuff in Codex. But how is it put together ?. On loop 1; I am trying to get single post from a single cat and on loop 2;- 5 posts say from another single cat ? All code or prompts / hints gratefully received. TY.

  2. ifelse
    Member
    Posted 9 years ago #

    You can try this;

    on the first loop, do as you would normally. On the second loop however, you create a new WP_Query object (which is what handles the actions and properties for the_loop) via:
    <?php $sub_query = new WP_Query('category_name=site&showposts=5'); ?>

    Now you can loop through this wp_query object as you would normally:
    <?php while ($sub_query->have_posts()) : $sub_query->the_post(); ?>
    <?php
    the_excerpt();
    ?>
    <?php endwhile; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags