Create an array() from ACF repeater

时间:2015-12-10 01:39:46

标签: arrays wordpress advanced-custom-fields

I'm trying to create several arrays of data from a repeater. There are 3 columns to each row;

  1. bus_type
  2. band_food
  3. band_no_food

The first col is TEXT and the other 2 are select lists.

What I'd like to do is loop through the results and depending on the condition, store the "bus_type" value in an array whihc I can later use as a conditional statement. I thought something like this would work, but not having any luck;

$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;

Any ideas hows I would achieve the desired result?

SOLVED

OK, it looks like storing this as a VAR works. The final code would look like this;

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 个答案:

答案 0 :(得分:1)

这可能有用。它遍历给定的转发器字段,检查该值是否与 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 conditon and register bus type in array
            if( the_sub_field('band_food') == 'hamburger' ):
               $whihc[] = the_sub_field('bus_type');
            endif;

        endwhile;

endif;

?>

大部分代码都来自此处:http://www.advancedcustomfields.com/resources/repeater/