从ACF重复器创建一个数组()

3

我试图从一个repeater创建几个数据数组。每行有3列:

  1. bus_type(文本)
  2. band_food(选择列表)
  3. band_no_food(选择列表)

我想做的是循环遍历结果,根据条件将“bus_type”值存储在数组中,以便稍后可以用作条件语句。我认为下面这样做应该可以,但没有成功;

$foo_a = array();
$foo_b = array();

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

    if (get_sub_field('band_food') == 'a') {
        $foo_a[] = get_sub_field('bus_type');
    } else if (get_sub_field('band_food') == 'b') {
        $foo_b[] = get_sub_field('bus_type');
    }

endwhile; endif;

有没有想法如何实现所需结果?
已解决
好的,看起来将其存储为VAR可以起作用。最终代码应该是这样的;
if( have_rows('bus_type') ): while ( have_rows('bus_type') ) : the_row();

    $food_var = get_sub_field('band_food');

    if ($food_var == 'a') {
        $foo_a[] = get_sub_field('bus_type');
    } else if ($food_var == 'b') {
        $foo_b[] = get_sub_field('bus_type');
    }

endwhile; endif;
1个回答

2
这可能有效。它循环遍历给定的重复器字段,检查值是否匹配“hamburger”,如果有匹配,则在数组whihc中注册公交车类型。
<?php
$whihc = array();

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

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

            // check for some condition and register bus type in array.
            if( the_sub_field('band_food') == 'hamburger' ):
               $whihc[] = get_sub_field('bus_type');
            endif;
        
        endwhile;

endif;

?>

这里的大部分代码来自于这个网址:http://www.advancedcustomfields.com/resources/repeater/


1
很遗憾,这基本上就是我提供的示例中所包含的,除了此代码将输出数据外,因此使用get_sub_field。似乎该条件不起作用,因为删除它会将数据添加到数组中。 - user1235285
1
使用 get_sub_field(返回值)代替 the_sub_field(输出值)。 - Nadav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接