【问题标题】:ACF loop Repeater values with get_field带有 get_field 的 ACF 循环中继器值
【发布时间】:2016-05-01 14:20:18
【问题描述】:

我使用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/

我不知道我将使用中继器创建多少字段,我想使用 foreach 循环所有值。这可能吗?

提前谢谢你

【问题讨论】:

  • 您找到的代码将循环来自转发器字段的所有值,只需将 field_name 更改为您的字段名称(slug 名称)即可。
  • 不行! :(我输入了我的repeater_field_name:get_field('MY_repeater_field_name');和我的field_name:$row['MY_sub_field_1'],但它不起作用。它返回给我这个:sub_field_1 = , sub_field_2 = , etc sub_field_1 = , sub_field_2 = , etc
  • 可能是个愚蠢的问题,但您在帖子中添加了一些内容吗?因为它似乎有效,但字段中没有任何价值?
  • 是的,当然。我在自定义字段中放置了一些值。使用转发器,我创建了两个自定义字段。还有其他方法可以显示这些值吗?
  • 我还发现了这段代码:link 在使用数组值下。我输入了我的 Repeater_field_name,它返回给我这个:array(2) { [0]=&gt; array(1) { ["testo"]=&gt; string(10) "My Value 1" } [1]=&gt; array(1) { ["testo"]=&gt; string(10) "My Value 2" } } 那么这段代码可以帮助我吗?

标签: php wordpress advanced-custom-fields


【解决方案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;

?>

【讨论】:

    【解决方案2】:

    我会这样修复它:

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

    【讨论】:

      猜你喜欢
      • 2017-06-08
      • 2019-02-15
      • 2019-11-03
      • 2014-09-13
      • 2014-07-17
      • 2019-11-16
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      相关资源
      最近更新 更多