0

I'm trying to create an interface on the admin that will allow our authors to select top resources to place as an aside on our archive page. I'm trying to add a loop in addition to the original query run on the archive page. I have

  1. set up a page with a repeater field
  2. referenced this page in the archive to grab the repeater info (by naming the repeater the same as a taxonomy slug and using get_queried_object()->slug.

I'm running into some interference between the two loops:

archive.php:

//REPEATER LOOP

<?php 
$slug = get_queried_object()->slug;
$posts = get_field($slug, 156053); //returns the post objects from the page with the repeaters

if( $posts ): ?>

    <ul>
    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
        <?php setup_postdata($post); ?>
        //post markup
    <?php endforeach; ?>
    </ul>
    <?php wp_reset_postdata(); ?>

<?php endif; ?>

//ORIGINAL ARCHIVE LOOP

<?php while (have_posts()) : the_post(); ?>
  <?php get_template_part('templates/content', get_post_format()); ?>
<?php endwhile; ?>

In this case, the first loop works as intended, and the original archive loop returns correct number of posts, but the postdata is from the original loop. (i.e. the first loop returns 3 results, the second loop returns 26 results but the first 3 are the same as the 3 from the original loop and the remainder are blank).

I have found that changing the of the repeater loop variable $post takes away the conflict with the original archive loop, but then the repeater loop brings in the first post of the archive loop (x3, the number of posts designated by the repeater)

I'm really scratching my head over this one. Any help greatly appreciated!

  • "... first loop works as intended, and the original archive loop returns correct number of posts, but the postdata is from the original loop." -- Sorry, aren't "first" and "original" the same thing? – s_ha_dum May 2 '14 at 13:49
  • sorry that was ambiguous. By "first" I meant the repeater loop – psorensen May 2 '14 at 13:54
  • It is still not clear. – s_ha_dum May 2 '14 at 14:02
  • Really this question could be simplified to 'how does one include an additional get_posts loop on the archive.php template?' archive inherently has the wordpress loop, but I want to maintain while adding an additional. – psorensen May 2 '14 at 14:08
1

I'm with s_ha_dum that this isn't very clear on what's happening, but have you tried wp_reset_postdata()? I believe you would put it before the last while loop in your example above.

Here's an article on other methods of resetting the query if that's not working: http://digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop/

  • moving the reset out of the (repeater loop) if statement worked! Thanks for digging in even if it was unclear. – psorensen May 2 '14 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.