ACF 中继器字段不同的计数


ACF Repeater Fields different counts

我有一段代码:

<?php
if( have_rows('our_people') ):
$i = 0;
while ( have_rows('our_people') ) : the_row(); ?>
    <?php $i++; ?>
    <?php if( $i > 3 ):
        break; ?>
    <?php endif; ?>
    <a class="people-thumb fancybox-inline" href="#fancybox-<?php echo $i;?>">
                <div class="people-thumb-image" style="width:172px;height:172px;overflow:hidden;background:transparent url(<?php the_sub_field('people_image'); ?>) no-repeat center center; background-size:cover;"></div>
                <div class="people-thumb-details">
                    <h3><?php the_sub_field('people_name_acf'); ?></h3>
                    <p><?php the_sub_field('people_title'); ?></p>
                </div>
    </a>
    <div class="peopleoverlay">
        <div id="fancybox-<?php echo $i;?>">
        <img src="<?php the_sub_field('people_image'); ?>" width="172" style="float:left;margin:0 10px 10px 0;"> <?php the_sub_field('people_details'); ?>
        </div>
    </div>
<?php endwhile;    
 else :
 endif;
?>

这样做只是计算条目,然后在 3 后停止 - 工作正常。

但是,对于中继器字段中的其他行(3 之后(,如何让它们也显示?原因是这 3 个条目位于单独的样式div 中,所有其他条目都放置在另一个div 中。

但是,如果我尝试重复此代码,则不会显示任何内容。所以我必须以某种方式重置它?我希望 3 之后的所有中继器行都显示在不同的部分中。

看起来您的代码明确表示不要显示超过 3 个结果!注释如下。

$i = 0; // Start a counter
while ( have_rows('our_people') ) : the_row(); ?>   // As long as there are posts to display
<?php $i++; ?>         // increment the counter
<?php if( $i > 3 ):    // if the counter is 4 or more...
    break; ?>              // don't execute the code below; jump out of the while loop.
<?php endif; ?>

话虽如此,请删除$i = 0 ,以及以下行:

<?php $i++; ?>
<?php if( $i > 3 ):
    break; ?>
<?php endif; ?>

这将允许 ACF 显示存储在该元字段中的所有人。

但是,如果要将不同的类添加到第 4 个div 及更高版本,请更改条件,删除 break 语句,并将结果包装在标识它的div 中。

<?php $i++; ?>
<?php if( $i > 3 ): ?>
    <div class="other-style">
<?php endif; ?>

然后,您必须在最后用另一次检查关闭它。

...
<?php if( $i > 3 ): ?>
    </div> <!-- .other-style -->
<?php endif; ?>