0
votes

I have a repeater field in wich I have a radio field. I need to output the label and the value.

In ACF field, I putted value:label such as : red: Red Carpet green: Green leaf

I tried a piece of code :

$field = get_sub_field_object(‘field_name’);
$value = get_sub_field(‘field_name’);
$label = $field[‘choices’][ $value ];

I tried to replace the fieldname by the field_id, but it returns "Array" instead of the value.

I need to use the value in a class, and the label in a title. Can you help me ?

1

1 Answers

0
votes

get_sub_field_object() has to be used within a has_sub_field() loop, such as this:

<?php while( has_sub_field('repeater_fields_name') ): ?>

    <?php 

    // vars
    $select = get_sub_field_object('radio_field_from_your_code');
    $value = get_sub_field('radio_field_from_your_code');

    ?>
    <ul>
        <?php foreach( $select['choices'] as $k => $v ): ?>
            <li>
                <?php if( $k == $value ): ?>
                    <span class="selected">Selected!</span>
                <?php endif; ?>
                <?php echo $v; ?>
            </li>
        <?php endforeach; ?>
    </ul>

<?php endwhile; ?>

You're probably close to getting the right values. Just tweak things to follow this general pattern. More info on this function can be found at ACF's docs site: https://www.advancedcustomfields.com/resources/get_sub_field_object/