Last active
January 23, 2018 11:19
-
-
Save webgurus/44d0d5428a3ee42ed4ebf386e47ca6aa to your computer and use it in GitHub Desktop.
Loop with row after each second item
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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