ACF循环中继器值与get_field

时间:2016-01-25 10:07:06

标签: php wordpress advanced-custom-fields

我创建了一个带有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循环所有值。这可能吗?

提前谢谢

enter image description here enter image description here

2 个答案:

答案 0 :(得分:1)

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;

?>

答案 1 :(得分:0)

我会这样解决:

<?php
if( have_rows('slide') ): 
  $l= 1;
  while( have_rows('slide') ): the_row();       
    $l++;
  endwhile; 
endif;  
?>
相关问题