• I have this code below. What I’m trying to do is contain each pair of 2 posts in between the <div class”ef-slide”> How would I do that?

    <div class="ef-slide">
    <?php 
    
    $recent_blog_posts = new WP_Query('posts_per_page=6');
    
    if ($recent_blog_posts->have_posts()) :
    										while ($recent_blog_posts->have_posts()) : $recent_blog_posts->the_post();
    											get_template_part( 'content' );
    										endwhile;
    
    else : echo "No Posts";
    									endif;
    
    ?>
Viewing 1 replies (of 1 total)
  • This is where you use a modulus operator. Your code should look something like this:

    <div class="ef-slide">
        <?php
    
        $recent_blog_posts = new WP_Query('posts_per_page=6');
    
        if ($recent_blog_posts->have_posts()) :
            while ($recent_blog_posts->have_posts()) : $recent_blog_posts->the_post();
                get_template_part( 'content' );
                // if multiple of 2 close the .ef-slide div and open a new one
                if($i % 2 == 0) {echo '</div><div class="ef-slide">';}
            endwhile;
    
        else : echo "No Posts";
        endif;
    
        ?>
    </div>
Viewing 1 replies (of 1 total)
  • The topic ‘[The Loop] Put every 2 posts in a div’ is closed to new replies.