Home › Forums › ACF PRO › Display all checkbox options but add class to selected › Reply To: Display all checkbox options but add class to selected
Sorry forgot to show you what I have tried so far. I found someone asking a similar question in forum https://support.advancedcustomfields.com/forums/topic/checkbox-how-to-display-all-item-as-check-uncheck/
I used the code in the reply marked as solved, but are getting errors.
Here’s my PHP code:
<?php
$value = get_field('service_options');
$field = get_field_object('service_options');
$choices = $field['choices'];
if( $field ) :
?>
<ul>
<?php
foreach($choices as $value => $label) :
if( in_array($value, $values) ) :
$class = ' class="selected"';
else :
$class = '';
endif;
echo '<li' . $class . '>' . $label . '</li>';
endforeach;
?>
</ul>
<?php endif; ?>
But I’m getting the following warning for each option:
Warning
: in_array() expects parameter 2 to be array, null given in …
In addition to the warning on each item, , the class selected isn’t being added to the options that were selected.
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.