ACF Wordpress 组内的转发器

Repeater inside Group ACF Wordpress

我正在使用 Wordpress 的高级自定义字段并尝试在组内循环转发器。我得到的只是 "Notice: Array to string conversion in..."

出了什么问题,我该如何解决?

<?php if( have_rows('start_horlurar') ): while ( have_rows('start_horlurar') ) : the_row();  ?>

<?php $horlur = get_sub_field('horlur'); ?>

<?php if( have_rows( $horlur['arsmodeller_lankar']) ): while ( have_rows($horlur['arsmodeller_lankar']) ) : the_row();  ?>

<?php echo get_sub_field('lank'); ?>

<?php endwhile; endif; ?>

<?php endwhile; endif; ?>

在嵌套的ACF中继器中,您不需要添加父中继器的引用——只需单独添加中继器名称。像这样尝试。

<?php
if( have_rows('start_horlurar') ): while ( have_rows('start_horlurar') ) : the_row(); 
    echo get_sub_field('horlur');
    if( have_rows('arsmodeller_lankar') ): while ( have_rows('arsmodeller_lankar') ) : the_row(); 
        echo get_sub_field('lank');
    endwhile; endif;
endwhile; endif;
?>

更新代码: 您也需要像 ACF Repeater 一样循环 ACF Group 字段。像这样尝试。

<?php
if( have_rows('start_horlurar') ): while ( have_rows('start_horlurar') ) : the_row(); 
    if( have_rows('horlur') ): while ( have_rows('horlur') ) : the_row();       
        if( have_rows('arsmodeller_lankar') ): while ( have_rows('arsmodeller_lankar') ) : the_row(); 
            echo get_sub_field('lank');
        endwhile; endif;
    endwhile; endif;
endwhile; endif;
?>

我相信这个回答是正确的,但对于那些寻找通用实现的人来说似乎不够清楚。

<?php

if( have_rows('your_group') ): while ( have_rows('your_group') ) : the_row(); 

    if( have_rows('your_repeater') ): while ( have_rows('your_repeater') ) : the_row();       

        echo get_sub_field('repeater_sub_field');

    endwhile; endif;

endwhile; endif;

?>

通常对于群组,您可以通过以下方式访问特定的子字段:

<?php 

$group_var = get_field['your_group']; 

$group_sub_field_var = $group_var['group_sub_field']

?>

但是,对于嵌套在组内的中继器,您似乎无法使用此策略,并且必须先使用 have_rows() 循环遍历一个组才能到达中继器。

如果您查看 group documentation on ACF it mentions how looping through a group is done like a repeater. Also the have_rows() documentation 有更多关于使用 have_rows() 的嵌套循环的详细信息。

我发现双循环很乱而且不需要。 我意识到这已经过时了,但我只是遇到了这个问题,不想有两个循环。

对于我的组 ('group') 和我的转发器 ('repeater'),子字段 ('subfield') 这就是我所做的。

     $group = get_field('group');
     $repeaters = $group['repeaters'];
     foreach($repeaters as $repeater) {
         echo $repeater["subfield"];
       }

超级简单,而且干净多了。如果需要,您可以添加 'if' 语句,而不是控制我的必填字段。

我发现这种方法对于快速和肮脏很重要。我对几乎所有内容都使用组,以便能够为后端的自定义字段创建更好的用户体验。我的大部分自定义字段都分组并获取参数,我希望它的代码尽可能少且尽可能干净。

如果你们发现此方法有任何问题,请告诉我,尤其是在性能方面。