Support

Account

Home Forums Add-ons Repeater Field Repeater field and checkbox

Solving

Repeater field and checkbox

  • Hello,
    I’m using repeater field within I put a ckeckbox field type.
    I can’t find a way to get the value of each subfield.
    The goal is that each subfield will displays differently based on what checkbox has been ticked off.

    Any help would be really appreciated.
    🙂
    Cheers.

  • Hi @whoaloic

    Hmm… A checkbox field will return an array of the selected values. You could use in_array() function to to determine if a certain value is within the array as shown below:

    if( have_rows('repeater_field_name') ):
    
     
        while ( have_rows('repeater_field_name') ) : the_row();
    
            $value = get_sub_field('checkbox_field_name');
    
            if(in_array('value1', $value)) {
            	// display subfields
            } else if(in_array('value2', $value)) {
            	// display other subfields
            }
    
        endwhile;
    
    else :
    
        // no rows found
    
    endif;

    For more info, check out:http://php.net/manual/en/function.in-array.php

  • Thank you for your answer!
    It solves the Conditional statement.
    But how to display selected values labels?
    I tried echo $value; but it displays only “Array”.

    I checked the doc: http://www.advancedcustomfields.com/resources/checkbox and find this:

    /*
    *  Displaying a single value's Label
    */
    
    $field = get_field_object('field_name');
    $value = get_field('field_name');
    $label = $field['choices'][ $value ];
    

    But I don’t understand how the $label works? Where [‘choices’] comes from?

  • Hi @whoaloic

    Well, in that case use get_sub_field_object() instead of get_sub_field() function. i.e.

    $sub_field_object = get_sub_field_object('checkbox_field_name');
    
    $value = $sub_field_object['value']; // values
    $label = $sub_field_object['label']; // labels

    Have a look at the documentation for get_sub_field_object() for more info: http://www.advancedcustomfields.com/resources/get_sub_field_object/

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Repeater field and checkbox’ is closed to new replies.