Support

Account

Home Forums ACF PRO Checkbox values not allowing conditional display

Solved

Checkbox values not allowing conditional display

  • I have an ACF checkbox sub field in a group field on an options page. It has 4 options, and is set to return “Value”.

    I am trying to make a series of radio buttons that correspond to these options in various places around a site. These should display content only for the choices that have been checked.

    Using the method given under “Conditional logic” at https://www.advancedcustomfields.com/resources/checkbox/ I cannot get the unchecked options not to show. No matter the selection all 4 options are always visible.

    Here’s the code I’m using (checkbox field must be stored as a variable since it is in an options page):

    <?php
    if(have_rows(‘currencies’, ‘option’)): while(have_rows(‘currencies’, ‘option’)): the_row();
    $currency = get_sub_field(‘currency’);
    endwhile; else : endif;
    ?>

    <?php 
        if(have_rows('currencies', 'option')): while(have_rows('currencies', 'option')): the_row();
            $currency = get_sub_field('currency');
        endwhile; else : endif;
    ?>
    
    <form class="currencies" name="currForm">
        <?php if($currency && in_array('GBP', $currency)): ?>
            <input type="radio" id="gbp" name="curr" value="gbp" checked>
            <label for="gbp">GBP<div class="highlight"></div></label>
        <?php endif; ?>
        <?php if($currency && in_array('ZAR', $currency)): ?>
            <input type="radio" id="zar" name="curr" value="zar" >
            <label for="zar">ZAR<div class="highlight"></div></label>
        <?php endif; ?>
        <?php if($currency && in_array('USD', $currency)): ?>
            <input type="radio" id="usd" name="curr" value="usd">
            <label for="usd">USD<div class="highlight"></div></label>
        <?php endif; ?>
        <?php if($currency && in_array('EUR', $currency)): ?>
            <input type="radio" id="eur" name="curr" value="eur" >
            <label for="eur">EUR<div class="highlight"></div></label>
        <?php endif; ?>
    </form>
    

    If I perform a var_dump on $currency I get:

    
    array (size=4)
      0 => string 'GBP' (length=3)
      1 => string 'ZAR' (length=3)
      2 => string 'USD' (length=3)
      3 => string 'EUR' (length=3)
    

    My ACF settings are in the attached screenshot.

    Here is a page where this is happening: https://bxd.dev.matmartin.studio/membership — The buttons marked GBP, ZAR, USD and EUR are generated by the code above. Currently the GBP, ZAR and EUR boxes are checked, so the USD button should not be visible (see attached images).

    I think I have used this approach before successfully – I may have overlooked something very obvious in this case but I’m not seeing it.

    Many thanks for any help.

  • I don’t see anything wrong based on what you’ve provided or why you are getting values in the list that have not been selected.

  • That’s both reassuring and frustrating! If even you can’t see the issue John, then we really are in trouble!

    Do you reckon there’s any chance completely rebuilding the fields and changing their name would help? I can’t help wondering if I’ve got something stuck in the DB from an old iteration that I’ve forgotten about…

  • not sure if I am looking at this wrong, but from what I see in the code, the dump of $currency shouldn’t be an array

    this bit here

    <?php 
        if(have_rows('currencies', 'option')): while(have_rows('currencies', 'option')): the_row();
            $currency = get_sub_field('currency');
        endwhile; else : endif;
    ?>

    This would overwrite the value of $currency with each iteration, try changing it to

    <?php 
        $currency = array();
        if(have_rows('currencies', 'option')): while(have_rows('currencies', 'option')): the_row();
            $currency[] = get_sub_field('currency');
        endwhile; else : endif;
    ?>

    and because currency is defined, you can eliminate the if($currency) and just use the in_array()

  • I don’t know.

    You can look in your the options table in the database. There should be 2 entries for each field.

    Group field
    option_name = “options_currencies” = acf field data
    option_name = “_options_currencies” = acf field key reference

    currency sub field
    option_name = “options_currencies_currency” = acf field data
    option_name = “_options_currencies_currency” = acf field key reference

    You could search the table for names like %options_currencies_% and look for any duplicates, or anything that does not conform to those.

    There could also be a conflict with something else, it could be completely possible for something else to be using the same option name.

  • Yes — very weird. A weekend away from it and some fresh eyes didn’t help much, I couldn’t get @kender’s approach to work either, but just re-creating the fields and going again has fixed the problem so I guess the database just had something else in it using the same terms…

    Thanks everyone for the help.

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

You must be logged in to reply to this topic.