Support

Account

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

Solved

Why are ['choices'] return all results ?

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

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

    But I get wrong resalt.
    https://i.ibb.co/3WTbLs7/Screenshot-4.png

    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.