Home › Forums › Front-end Issues › Display all checkbox options and apply class to those that are checked › Reply To: Display all checkbox options and apply class to those that are checked
Dang I was so close, thank you! That seems to solve the error I was getting and it’s displaying the checkbox labels now, but it’s also displaying most of the classes as “unavailable” when they should be “available”.
This one shows only one class as “available” when it should be all 7.
<?php
$tier = get_field('tier');
$values = $tier['available_features'];
$field = get_field_object('tier_available_features');
$choices = $field['choices'];
?>
<?php foreach($choices as $choice => $label) : ?>
<?php foreach ( $values as $value ) : ?>
<?php if( $value['value'] == $choice ) {
$status = 'available';
}
else {
$status = 'unavailable';
} ?>
<?php endforeach; ?>
<li class="<?php echo $status;?>"><?php echo $label;?></li>
<?php endforeach;?>
This version lists the checkbox labels correctly but marks them all with the “unavailable” class.
<?php
$tier = get_field('tier');
$values = $tier['available_features'];
$field = get_field_object('tier_available_features');
$choices = $field['choices'];
?>
<?php foreach($choices as $value => $label) : ?>
<?php if (in_array($value, $values)) {
$status = 'available';
}
else {
$status = 'unavailable';}?>
<li class="<?php echo $status;?>"><?php echo $label;?></li>
<?php endforeach;?>
What am I missing here to get it to appropriately return the value of the checkbox?
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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.