Support

Account

Home Forums General Issues Multiple conditional checkbox values not displaying data

Helping

Multiple conditional checkbox values not displaying data

  • I’m using the checkbox field to display data depending on what is selected.

    I’m getting data based on whether one checkbox or another is selected. Is it possible to show all data when all checkboxes are selected?

    Here’s an ideal example with two checkboxes:

    
    <?php $options = get_field('options');?>
    <?php if( $options && in_array('option-1', $options) ): ?>
      <p>Option 1</p>
    <?php elseif ( $options && in_array('option-2', $options) ): ?>
      <p>Option 2</p>
    <?php elseif ( $options && in_array('option-1', $options) && in_array('option-2', $options ): ?>
      <p>Option 1 and Option 2</p>
    <?php endif;?>
    

    Is this possible?

  • You need to do the more specific check first.

    Your first check is true if the 3rd one is true. Since the 1st one is true you will never get to the 3rd one.

    
    <?php $options = get_field('options');?>
    <?php if( $options && in_array('option-1', $options) && in_array('option-2', $options ) ): ?>
      <p>Option 1</p>
    <?php elseif ( $options && in_array('option-2', $options) ): ?>
      <p>Option 2</p>
    <?php elseif ( $options && in_array('option-1', $options) ): ?>
      <p>Option 1 and Option 2</p>
    <?php endif;?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.