Support

Account

Home Forums ACF PRO Select Field get multipe value AND label

Solved

Select Field get multipe value AND label

  • Hi everyone!

    I’ve created a select field with multiple select input.
    I would like to get the label as well as the value in a foreach loop.

    At the moment I use this code snippet:

    
    $field_name = "md_services";
    $field = get_field_object($field_name);  
    
    if( $field ){
    foreach( $field['choices'] as $k => $v ){  
    $icon = '<span class="md-service-img"> <img src="' . get_stylesheet_directory_uri() . '/images/icons/' . $k . '.png"><span class="md-service-desc">' . $v . '</span></span>'; 
    echo $icon;
    }
    }
     

    For example the select field “md_services” has 5 choices. I selected 3 of them.
    With this code it displays the image and description correctly – but for all 5 possible choices.

    If I change $field[‘choices’] to $field[‘value’] it displays only the 3 selected services but not the label. Of course if you check the field_object.

    Is it possible to get the value as well as the label of a multi select field?

    Thank you so much in advance!

  • the way to do that the code needs to look something like this:

    
    $field_name = "md_services";
    $values = get_field($field_name);
    $field = get_field_object($field_name);  
    
    if( $values ){
      foreach( $values as $value ){  
        $icon = '<span class="md-service-img"> <img src="' . 
                 get_stylesheet_directory_uri() . '/images/icons/' . 
                 $value . '.png"><span class="md-service-desc">' . 
                 $field['choices'][$value] . '</span></span>'; 
        echo $icon;
      }
    }
    

    Recently I have created some function that can do this as well and added them to my library of ACF filters and functions https://github.com/Hube2/acf-filters-and-functions/blob/master/acf-field-label-functions.php

  • Hey John,

    Great, it works perfectly!
    Thank you very much.

    Regards, Marc

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

The topic ‘Select Field get multipe value AND label’ is closed to new replies.