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 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.