Get select box values of advanced custom field

I am using a repeater field type of a field.The parent field name is ‘map_details’ and the sub field name is ‘name_of_county’ which is a select dropdown list.

Now I want to display all the values of that select box in the frontend.My code is

Read More
$field_key = "field_535befe551ba5"; //field key of parent
$field = get_field_object($field_key); 

if( $field )
{
    echo '<select name="' . $field['key'] . '">';
        foreach( $field['choices'] as $k => $v )
        {
            echo '<option value="' . $k . '">' . $v . '</option>';
        }
    echo '</select>';
}

it’s not working as I have given the field key of parent not the sub field which is basically a select box.Also I can’t find the field key for the sub field.

How can I display the sub field dropdown box with values and keys in the frontend.

Related posts

Leave a Reply

1 comment

  1. I recently find the way to output the values/keys in the frontend:
    In these cases, I always print_r the parent array (between ‘pre’ tags) in order to understand what’s going on, trust me… it helps.
    As in a repeater, the select field (sub_field) is one step deeper in the array, so:

    $field_key = "field_535befe551ba5"; //field key of parent
    $field = get_field_object($field_key); 
    
    if( $field ) {
    
        // As explained, it's better to first take a look to
        // the array's structure. Uncomment the next 3 lines
        // to print it in a pretty way
    
        // echo '<pre>';
        // print_r ($field);
        // echo '</pre>';
    
        foreach ($field['sub_fields'] as $the_subfield) {
            $the_subfield_name = $the_subfield['name'];
    
            // in case that you have more than one subfield in the repeater
            // we need to check that we are in the desired subfield:
            if ($the_subfield_name == 'name_of_the_subfield') {
            //in your case: 'name_of_country'
    
                // now that we are sure that we are inside the desired
                // subfield, we will output the different choices available
                echo '<select name="' . $the_subfield['name'] . '">';
                foreach( $the_subfield['choices'] as $k => $v ) {
                    echo '<option value="' . $k . '">' . $v . '</option>';
                }
                echo '</select>';
            }
        }
    }
    

    I hope it helps!