使用modulo
操作人员
PHP中的模签名是%
, 其工作原理如下:例如,它给出除法的余数
5 % 2 = 1
9 % 3 = 0
11 % 7 = 4
因此,您的代码应该是这样的(我认为您的问题中有一个输入错误,在“前三个”中,第二个块应该标记为
small right
. 如果我在这里错了,只需编辑下面的代码。)
为了更好地概述,我将计数器从1开始,而不是像往常一样从0开始,因为更容易看到条件所针对的迭代。我还增加了最后一个条件的计数器-请确保不要再增加两次!
因此,在循环之前:
$counter = 1;
然后插入循环:
<?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>\';
}
?>