For your clearfix wrapper you are doing it wrong. You have this:
$counter = 0;
while ( $loop->have_posts() ) : $loop->the_post();
if ($counter % 4 == 0) :
echo $counter > 0 ? "</div>" : ""; // close div if it's not the first
echo "<div class="clearfix">";
endif;
// BUILDS INTERNAL DIVS
$counter++;
endwhile;
So you are opening the div for numbers 0 and multiples of the number 4. Then inside that statement you are only closing a div if the $counter
is greater than 0. So you end up closing the div, then opening a new one that may not get closed.
You need to do two things. First, move the closing div
to echo at the end of the while
statement. Second, wrap the closing div
in a different conditional. Here it is corrected:
$counter = 0;
while ( $loop->have_posts() ) : $loop->the_post();
if ($counter % 4 == 0) {
echo "<div class="clearfix">";
}
// BUILDS INTERNAL DIVS
/**
* Closing div for loop
*
* $counter is more than 0 AND $counter plus 1 is multiple of 4
* OR $counter plus 1 is the total posts found (the plus 1 here is because we start with 0 which counts toward our total)
*/
if ( ($counter > 0 && ($counter+1) % 4 == 0 ) || ($counter+1) == $loop->found_posts ) {
echo "</div>";
}
$counter++;
endwhile;