2020-06-28 / 1535阅 / 悠然
返回have_rows()
循环中的当前行索引。
在遍历“中继器”或“灵活内容”字段的行时,您可能会发现有必要确定行号(索引)。此功能正是这样做的,避免了需要自定义$i++
计数器的情况。
(int)当前行的数字索引。请参阅有关索引偏移量的注释。
此示例演示如何使用此函数将唯一ID输出到每行的包装器中。这对于CSS / JS定制很有用。
<?php if( have_rows('slides') ): ?>
<?php while( have_rows('slides') ): the_row(); ?>
<div class="accordion" id="accordion-<?php echo get_row_index(); ?>">
<h3><?php the_sub_field('title'); ?></h3>
<?php the_sub_field('text'); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<div class="accordion" id="accordion-1">
<h3>My first accordion</h3>
<p>Some text here.</p>
</div>
<div class="accordion" id="accordion-2">
<h3>My second accordion</h3>
<p>Some moretext here.</p>
</div>
从此函数返回的索引从1开始。这意味着具有3行数据的Repeater字段将产生1、2和3的索引。
要从0开始索引,请像这样使用row_index_offset设置。
add_filter('acf/settings/row_index_offset', '__return_zero');