简体   繁体   English

如何在ACF转发器字段中的行上循环并回显值编号

[英]How to loop through and echo value number on rows within the ACF repeater field

I want to print the count of how many rows are in the ACF repeater row. 我想打印ACF转发器行中有多少行的计数。 I have a foreach loop setup but I can't figure out what I'm missing. 我有一个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; ?>

Example output if there are 3 rows of data in the ACF fields. 如果ACF字段中有3行数据,则输出示例。

Items: 1
Items: 2
Items: 3

Try these code: 尝试以下代码:

<?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; ?>

Note, if the key of array is not number, you need to change the code like: 注意,如果数组的键不是数字,则需要更改代码,例如:

<?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; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM