You can do this with much less code, using the modulo
operator.
The modulo sign in PHP is %
, and works like this: it gives you the remainder of a division, for example
5 % 2 = 1
9 % 3 = 0
11 % 7 = 4
So your code would look like this (I think you have a typo in your question, in the “First Three” the second block should be labeled small right
. If I am wrong here, just edit the code below.)
For a better Overview, I start my Counter at 1, not as usual at 0, because it is easier to see which iteration is targeted by the conditionals. I also increase the counter at the last conditional – be sure you do not increment it twice!
So, before the Loop:
$counter = 1;
Then insinde the Loop:
<?php
if ( $counter % 3 == 1 ) { // use this line for the first and the fourth iteration
echo '<div class="layer">';
}
if ( $counter % 6 == 1 ) { // use this line for the first iteration only
echo '<div class="large left">';
} else if ( $counter % 6 == 2 ) { // use this line for the second iteration only
echo '<div class="small right">';
} else if ( $counter % 6 == 4 ) { // use this line for the fourth iteration only
echo '<div class="small left">';
} else if ( $counter % 6 == 5 ) { // use this line for the fifth iteration only
echo '<div class="large right">';
}
//nothing to do for the sixth and the third iteration
// the item is the same all the time
?>
<div class="item">
<?php the_title(); ?>
<?php the_content(); ?>
</div><!--item-->
<?php
if ( $counter % 6 == 1 || $counter % 6 == 3 || $counter % 6 == 5 || $counter % 6 == 0 ) { // use this line everytime you close a subblock
echo '</div>';
}
if ( $counter++ % 3 == 0 ) { // use this line for the third and the sixth iteration
echo '</div>';
}
?>