首页 WordPress函数大全 get_row_index()

get_row_index()

2020-06-28 / 1390阅 / 悠然

如何你看完本文还不知道如何使用该函数,可以联系我定制视频教程,50元/个函数,学不会全额退款

本视频需要登录以后才能播放

登录

描述

返回have_rows()循环中的当前行索引。

在遍历“中继器”或“灵活内容”字段的行时,您可能会发现有必要确定行号(索引)。此功能正是这样做的,避免了需要自定义$i++计数器的情况。

返回

(int)当前行的数字索引。请参阅有关索引偏移量的注释。

变更日志

  • 在版本5.3.4中添加

此示例演示如何使用此函数将唯一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设置。

functions.php

add_filter('acf/settings/row_index_offset', '__return_zero'); 
大家谈论
    我的见解