Support » Fixing WordPress » Multiple Loops – Embedded Loop

  • I have read http://codex.wordpress.org/The_Loop#Multiple_Loops and all the posts in the forum dealing with multiple loops — and still can’t get my embedded loop to work properly.

    Loop1 (Outerloop) is the WP basic loop and retrieves only 8 posts at a time on a page, with pagination to navigate to the prev/next page.

    Inside this loop, I embed a second loop to retrieve all titles for a certain category. To do this, I used a new query:

    <!-- Loop1 -->
    <?php if (have_posts()) : ?>
      <?php while (have_posts()) : the_post(); ?>
    
        <?php $my_query = new WP_Query(array(
        'category_name'=>press_releases,
        'year'=>date('Y'),
        'order_by'=>date,
        'order'=>'DESC',)); ?>
    
        <!-- Loop2 -->
        <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <?php endwhile; ?>
    
      <?php endwhile; ?>
    <?php endif; ?>

    Instead of getting all the titles for that category for the whole year, all I get are the titles for that category for the posts retrieved in Loop1.

    Of course, I do not want to mess up pagination for Loop1.

    Thanks in advance for any help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Try this for your “second loop to retrieve all titles for a certain category”

    <?php
    // posts from this year, in category id 1, exclude the current posts, display 5 posts, ignore sticky posts
        $args=array(
          'year'=>date('Y'),
          'cat' => 1,
          'post__not_in' => array($post->ID),
          'numberposts'=>5,
          'caller_get_posts'=>1
        );
    $catposts=get_posts($args);
    if ($catposts) {
    foreach($catposts as $catpost) {
    echo "<p><a href='".get_permalink($catpost->ID)."'>".$catpost->post_title."</a></p>";
    }
    }
    ?>

    Thanks, will try

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Multiple Loops – Embedded Loop’ is closed to new replies.