Skip to content

Instantly share code, notes, and snippets.

@webgurus
Last active January 23, 2018 11:19
Show Gist options
  • Select an option

  • Save webgurus/44d0d5428a3ee42ed4ebf386e47ca6aa to your computer and use it in GitHub Desktop.

Select an option

Save webgurus/44d0d5428a3ee42ed4ebf386e47ca6aa to your computer and use it in GitHub Desktop.
Loop with row after each second item
<?php
$post = new WP_Query($args);
$counter = 0;
$start_el = '<div class="row">';
$end_el = '</div><!-- .row -->';
$item_per_row = 3;
if ( $post->have_posts() ) :
// Opening the first row
echo $start_el;
while ($post->have_posts()) : $post->the_post();
echo ( ( $counter % $item_per_row === 0 ) && $counter !== 0 ) ? $end_el . $start_el : ''; ?>
<div class="col-sm-6">
<h2>Content</h2>
</div>
<!-- .col-sm-6 -->
<?php
$counter++;
endwhile;
wp_reset_postdata();
wp_reset_query();
// Closing the row
echo $end_el;
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment