Home Forums General Issues Why are ['choices'] return all results ?


Why are ['choices'] return all results ?

  • Hi. I try return all checked keys and values of my checkbox function. I use this code

      $countries = get_field_object('supported_countries', $postID);
     if( $countries['choices'] ): ?>
       <?php foreach( $countries['choices'] as $value => $label ) : ?>
          <li class="country <?php echo $value; ?>"><?php echo $label; ?></li>
            <?php endforeach; ?>
    <?php endif; ?>

    But I get wrong resalt.

    Can you help make this function correct? How can I get only the selected items?

  • The field object choices contains all of the possible choices for your field, the field value contains the choices that have been made.

    $field_object = get_field_object('field_name');
    $field_values = get_field('field_name');
    foreach ($field_values as $value) {
      <li class="<?php 
          echo $value; ?>"><?php 
          echo $field_object['choices'][$value]; ?></li>
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.