1

I'm trying to achieve simple result. Run main loop 2 times, but first time for the one post with different layout, and second time for the rest posts with different layout. I found that condition 1 > $wp_query->current_post can show only the first post in my loop. But when I run loop again it starts with 3rd post and skipping the 2nd.

Here is the code:

<!-- Full Post -->
<?php if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        // first post
        if( 1 > $wp_query->current_post ):
            get_template_part( 'template-parts/content', get_post_format() );
        else :
            break;
        endif;
    endwhile;
?>

<!-- Small Posts -->
<div class="row">
    <?php
      while( have_posts() ) : the_post();    
        get_template_part( 'template-parts/post-small', get_post_format() );      
      endwhile;
    ?>
</div>
1
  • a: there is usually no need to run the loop twice just to show the first post different; the conditional would be enough to use a different template part for the first post, and would also work to insert the additional html tags. b: if running the loop twice, use rewind_posts() codex.wordpress.org/Function_Reference/rewind_posts between the loops.
    – Michael
    Jul 11, 2017 at 15:33

2 Answers 2

-1

I think you need to put wp_reset_postdata(); after your first endwhile; so that the loop can return to the start. Can see more about that function here: https://codex.wordpress.org/Function_Reference/wp_reset_postdata

1
  • As I know wp_reset_postdata(); will reset only secondary loop, not the main loop.
    – Alexander
    Jul 11, 2017 at 9:35
-1

Try reading up here: https://codex.wordpress.org/The_Loop#Multiple_Loops and in particular the section here: https://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action and the use of: $do_not_duplicate

3
  • In the example you provided they use custom second loop. I know that's easy way, but I'm trying to use only main query here.
    – Alexander
    Jul 11, 2017 at 12:59
  • OK - then look at: post__not_in here: codex.wordpress.org/Class_Reference/… eg: $second_query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => $first_query ) );
    – TomC
    Jul 11, 2017 at 14:54
  • But again, it's an example with custom WP loop, which I try to avoid. The main query is if ( have_posts() ) : ( have_posts() ) : the_post(); which I try to modify and it works, but for some reason WordPress skips second post in query and start with third.
    – Alexander
    Jul 11, 2017 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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