【问题标题】:How to loop through and echo value number on rows within the ACF repeater field如何在 ACF 转发器字段中的行上循环和回显值编号
【发布时间】:2019-11-10 20:43:18
【问题描述】:

我想打印 ACF 转发器行中的行数。我有一个 foreach 循环设置,但我不知道我缺少什么。

<?php if( have_rows('repeater_section') ): ?>
    <ul>
        <?php while( have_rows('repeater_section') ): the_row(); ?>
            <li>
                $items = get_field('items', 'option');
                <?php
                    foreach ($items as item) {
                        if (i=0; i<=items.length)
                ?>
                        <p>Items: <?php $i;?></p>
                <?php
                    }
                ?>
            </li>
        <?php endwhile; ?>
    </ul>
<?php endif; ?>

如果 ACF 字段中有 3 行数据,则输出示例。

Items: 1
Items: 2
Items: 3

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    试试这些代码:

    <?php if( have_rows('repeater_section') ): ?>
        <ul>
            <?php while( have_rows('repeater_section') ): the_row(); ?>
                <li>
                    <?php
                        $items = get_field('items', 'option');
                        foreach ($items as $i => $item) {
                            ?>
                            <p>Items: <?php echo $i + 1;?></p>
                            <?php
                        }
                    ?>
                </li>
            <?php endwhile; ?>
        </ul>
    <?php endif; ?>
    

    注意,如果数组的key不是数字,则需要修改代码如下:

    <?php if( have_rows('repeater_section') ): ?>
        <ul>
            <?php while( have_rows('repeater_section') ): the_row(); ?>
                <li>
                    <?php
                        $i = 1;
                        $items = get_field('items', 'option');
                        foreach ($items as $item) {
                            ?>
                            <p>Items: <?php echo $i ++;?></p>
                            <?php
                        }
                    ?>
                </li>
            <?php endwhile; ?>
        </ul>
    <?php endif; ?>
    

    【讨论】:

      猜你喜欢
      • 2020-05-20
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 2021-06-23
      • 2019-03-19
      • 2021-04-26
      相关资源
      最近更新 更多