Support

Account

Home Forums Front-end Issues Displaying multiple select field labels

Solved

Displaying multiple select field labels

  • Hey there, I’ve been wracking my brain trying to figure out how to display multiple labels from a select field. Got it figured out and thought I’d contribute..

    $field = get_field_object('business_category');
    $value = implode(', ', get_field('business_category'));
    $label = $field['choices'][ $value ];
    echo $label
  • Hey wearegiants,

    thank you for this hint. Actually with your piece of code you only get the label of a single selected value.

    Use this code if you want to store all labels in one array ($labels):

    $labels = array();
    $field = get_field_object('fieldname');
    $values = get_field('fieldname');
    foreach ($values as $value) {
       $labels[] = $field['choices'][ $value ];
    }

    For outputting the labels:

    foreach ($labels as $label) {
       echo $label;
    }
  • This works great, but I’m having issues adding a space and a comma between the options and no comma if its just one item. Any ideas? (Im sure this has to be easier than I’m making it.)

  • Instead of doing this

    
    foreach ($labels as $label) {
       echo $label;
    }
    

    do this

    
    echo implode(', ', $labels);
    
  • This appears promising, but what if you are working with multiple field names and not just one? what if you have 8 fields that are multi select fields, out of 27 total fields that are other types?

  • I’m trying to apply this to an option page, but cannot get it to work.

    
    $labels = array();
    $field = get_field_object( 'sidebar_home', 'option' );
    $values = get_field( 'sidebar_home', 'option' );
    foreach ($values as $value) {
       $labels[] = $field['choices'][ $value ];
    }
    
    //And this is how I'm displaying it
    <?php echo implode(', ', $labels); ?>
    

    I’m getting an “Illegal offset type” error.

    My select field is set to multiple choices and a return format of “Both (Array)”.

    Any clues of why I’m getting that error?

    Thanks!

  • I’m getting the same “Illegal offset type” error, whether I try this with a single select item or multiple selected. I’ve tried every which online suggested way, removed all other plugins other than CPT UI (required), and with the latest (5.8.7) ACF Pro and standard editions. The error is thrown whether the return format is “Both (Array)” or not.

    If for instance I follow the instructions to Display value and label at https://www.advancedcustomfields.com/resources/select/https://www.advancedcustomfields.com/resources/select/, the offset error is on the line containing [‘choices’].

    And if I follow the Format value setting instructions, with the return format set to “Both (Array)”, the error is again thrown on the line containing [‘choices’]. If I change the word to something meaningless – [‘blah’] – there’s no illegal offset error, but of course no output there either.

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

The topic ‘Displaying multiple select field labels’ is closed to new replies.