Created
November 20, 2018 12:08
-
-
Save Farmatique/19f5199efa91fce46393a111c4a8bfd3 to your computer and use it in GitHub Desktop.
Wordpress PHP split (wrap) elements in array into rows
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 | |
$myposts = get_posts( $args ); //Array of posts | |
$index = 1; | |
$len = count($myposts); | |
foreach( $myposts as $post ): setup_postdata($post); | |
$post_id = $post->ID; | |
?> | |
<?php | |
if( $index%3 === 1): echo '<div class="row">'; //Open row for every 3-rd (change 3 to desired number in one row) | |
endif; | |
?> | |
<div class="single-element <?php echo $index; ?>"> | |
</div> | |
<?php | |
if( $index%3 === 0 || $index === $len): echo '</div><!-- end row -->'; // Close row for every 3rd (change to desired number) OR last | |
endif; | |
$index++; | |
endforeach; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment