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.
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>
}