Support

Account

Home Forums Front-end Issues Display all checkbox options and apply class to those that are checked

Solving

Display all checkbox options and apply class to those that are checked

  • I have a custom post type with checkboxes. I would like to show all the options on the frontend of the website, but add a class to those that are checked for a membership tier to showcase the available features.

    I have been trying to get this code to work but I keep getting an error “Trying to access array offset on value of type bool on line 110”. My checkbox field name is available_features and it is a sub field in a group name tier. I have tried both codes:

    Version 1:

    <?php
    $tier = get_field('tier');
    $values = get_field($tier['available_features']);
    $field = get_field_object($tier['available_features']); 
    $choices = $field['choices'];  // This is the line 110 that keeps throwing the error ?>
    
    <?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;?>

    Version 2:

    <?php
    $tier = get_field('tier');
    $values = get_field('available_features'); // This is the difference in V1 and V2
    $field = get_field_object('available_features'); // This is the difference in V1 and V2
    $choices = $field['choices']; // This is the line 110 that keeps throwing the error ?>
    
    <?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;?>

    I have also tried both with “get_sub_field” and “get_sub_field_object”. I keep running into the same error.

  • Getting values:
    Either this:

    
    $tier = get_field('tier');
    $values = $tier['available_features'];
    

    or this:

    
    $values = get_field('tier_available_features');
    

    or this:

    
    while(have_rows('tier')) {
      the_row();
      $values = get_sub_field('available_features');
    }
    

    getting the field object:
    this:

    
    $field = get_field_object('field_XXXXXXX'); // field key
    

    or

    
    $field = get_field_object('tier_available_features');
    

    or

    
    while(have_rows('tier')) {
      the_row();
      $field_object = get_sub_field_object('available_features');
    }
    
  • 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?

  • what is the return setting for the field? values, labels or both?

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.