Home › Forums › ACF PRO › Display all checkbox options but add class to selected › Reply To: Display all checkbox options but add class to selected
Alight, still having a conversation with myself lol.
I’m getting closer. I hadn’t seen the second code snippet John Huebner provided. This is the code I have now.
<?php
$values = get_field('service_options');
$field = get_field_object('service_options');
$choices = $field['choices'];
?>
<ul>
<?php
foreach($choices as $choice_value => $choice_label) :
foreach ( $values as $value ) :
if( $value['value'] == $choice_value ) :
$class = ' class="checked"';
else :
$class = '';
endif;
endforeach;
echo '<li' . $class . '>' . $choice_label . '</li>';
endforeach;
?>
</ul>
But it’s only working on one of the options. So if I check 3 options, it will only assign the ‘checked’ class to last checked option. So if I checked option 1, 2 and 4. The outputted html will only add the checked class to the option 4.
So close!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.