Support

Account

Home Forums Front-end Issues Checkbox output is displaying ALL only

Solved

Checkbox output is displaying ALL only

    • james

    • February 5, 2019 at 2:45 pm

    My checkbox code is displaying all the fields. I only need it to display the active/selected fields. Everything else in the code is working fine and have managed to wrap some class css so it spreads over 3 columns.

    // Current Code Used

    <?php
    $values = get_field(‘family_amenties’);
    $field = get_field_object(‘family_amenties’);
    $choices = $field[‘choices’];
    ?>

      <?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 . ‘‘;
      endforeach;

      ?>

    • james

    • February 5, 2019 at 2:47 pm
    This reply has been marked as private.
    • james

    • February 7, 2019 at 6:56 am

    Here is the fix if anyone is interested. This will output the selected checkbox items as a list.

    <?php
    
    // vars	
    $field = get_field_object('family_amenties');
    $values = $field['value'];
    
    // check
    if( $values ): ?>
    <ul>
    	<?php foreach( $values as $value ): ?>
    		<li><?php echo $field['choices'][ $value ]; ?></li>
    	<?php endforeach; ?>
    </ul>
    <?php endif; ?>
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

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.