繁体   English   中英

使用get_field的ACF循环中继器值

[英]ACF loop Repeater values with get_field

我创建了一个具有Repeater布局的“自定义字段”,以添加一些输入文本。 我想显示所有值。 我在ACF文档中找到了一些代码,但是我不明白它是如何工作的

<?php 
$rows = get_field('repeater_field_name');
if($rows)
{
    echo '<ul>';

    foreach($rows as $row)
    {
        echo '<li>sub_field_1 = ' . $row['sub_field_1'] . ', sub_field_2 = ' . $row['sub_field_2'] .', etc</li>';
    }

    echo '</ul>';
}
?>

http://www.advancedcustomfields.com/resources/repeater/

我不知道我将用Repeater创建多少个字段,我想用foreach循环所有值。 那可能吗?

先感谢您

在此处输入图片说明 在此处输入图片说明

Foreach版本:

<?php 

$rows = get_field('repeater');
if($rows)
{
    echo '<ul>';

    foreach($rows as $row)
    {
        echo '<li>sub_field_1 = ' . $row['text'] . '</li>';
    }

    echo '</ul>';
}

而版本:

<?php

// check if the repeater field has rows of data
if( have_rows('repeater') ):

    // loop through the rows of data
    while ( have_rows('repeater') ) : the_row();

        // display a sub field value
        the_sub_field('text');

    endwhile;

else :

    echo 'nothing found';

endif;

?>

暂无
暂无

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

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