Skip to content

Instantly share code, notes, and snippets.

@Farmatique
Created November 20, 2018 12:08
Show Gist options
  • Save Farmatique/19f5199efa91fce46393a111c4a8bfd3 to your computer and use it in GitHub Desktop.
Save Farmatique/19f5199efa91fce46393a111c4a8bfd3 to your computer and use it in GitHub Desktop.
Wordpress PHP split (wrap) elements in array into rows
<?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