Probably one of the more searched for items is how to insert a Clearing div every so many posts. In WordPress you’ll maybe run into this, where you wan to insert a Clearing div in a While Loop or a Foreach Loop. Below is an example for each and are both easily updated.
While:
<?php
$args = array(
'post_type' => 'any-post-type',
'posts_per_page' => 4,
);
$query = query_posts($args);
$i = 0; //start the count at 0
?>
<?php while (have_posts()) : the_post(); $i++// count through the posts ?>
<?php the_title();?>
<?php if( $i == 3): $i = 0; //if this is the 3rd post then clear! ?>
<div class="clear"></div>
<?php endif; ?>
<?php endwhile; wp_reset_query();?>
Foreach:
// CUSTOM POST TYPE OUTSIDE OF LOOP
<?php
$item_args = array(
'post_type' => 'anything',
'posts_per_page' => 5
);
$items = get_posts($item_args);
$count = 1; //start the count off
foreach($items as $item):
$item_id = $item->ID;
$somefield = get_field('somefield',$item_id);
?>
<?php echo $somefield; ?>
<?php if( $count % 3 == 0 ) echo "\n".'<div class="clear"></div>'; ?>
<?php $count++; endforeach;?>
<?php endif;?>