Support

Account

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!