如何从 PHP 中的循环计数器生成模数 3?

How to produce modulus 3 from loop counter in PHP?

能否设置一个计数器来分隔查询中的第一个项目,然后以 3 个为一组循环遍历其余项目?所以逻辑是:

item 1 - {separate id entirely}
item 2 class=1, item 3 class=2, item 4 class=3, RESET item 5 class=1, etc

我不确定如何为 class 进行计数减一,然后在每第 3 个项目后重置(项目 2、3、4 重置为 5、6、7 重置为 8, 9, 10).

我正在尝试删除 "featured block" ACF 行并将 ACF "content areas" 中的第一项设置为 "feature" 部分。这样,在内容编辑屏幕中,转发器行可以四处移动,而无需从一个部分到另一个部分重新创建内容。

<?php if( have_rows('featured_block') ): while( have_rows('featured_block') ): the_row(); if(get_row_layout() == 'feature' ): ?> 
<section class="feature"> ...feature stuff...</section>
<?php endif; ?><!-- end feature --> <?php endwhile; endif; ?><!-- end featured block -->
<section class="products clear">
    <?php $count = 0;
 // check for rows 
if( have_rows('content_areas') ): 
     ?>
<?php // add a counter
while( have_rows('content_areas') ): the_row(); $count++;  ?>
<article class="th<?php echo $count; ?> m1">
<?php // reset counter if 3 has been reached
    if ($count==3){
    $count=0;
    } ?> ...rows stuff...</article>
<?php endwhile; ?><?php  endif; // content_areas ?>
</section>

你可能在寻找类似的东西:

$counter = 0;
foreach( $items as $item ){

    if( $counter <= 1 ) continue;
    $class = ( ( ( $counter - 2 ) % 3 ) + 1 );
    $counter++;

}

所以你会得到这个 class 列表:

counter         class
-------         --------
1                nothing
2                1
3                2
4                3
5                1
6                2
7                3

我没有将转发器行中的第一个项目分开,而是添加了一个 true/false 如果一个项目应该成为特色。该行首先设置样式,然后其余行开始计数。这是代码

<?php $counter = 0;
         // check for rows 
    if( have_rows('repeater_name') ): 
           while( have_rows('repeater_name') ): the_row();  ?>
            <?php if ( get_sub_field( 'feature' ) ): ?><!--if the feature true/false checked to set true-->
                <section class="feature">
                    <!--pull in content area repeater rows that have feature set true-->
                </section>
        <section class="products clear"><!--if this is put after the else the section is repeated for each repeater-->
        <?php else: ?>

             <!--start counting the rest--> 
            <?php $counter++;  ?>   
                <article class="th<?php echo $counter; ?> m1">
    <?php // reset counter if 3 has been reached
        if ($counter==3){
        $counter=0;
        } ?>
                <!--pull in content area repeater rows that do NOT have feature set true-->
         </article>
            <?php endif; // end if have feature ?>
        <?php endwhile; // end repeater_name ?>
        </section>
<?php  endif; // repeater_name ?>