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

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