Advanced Custom Fields – Check if multiple get_fields exist?

I can only really seem to find documentation on wether one get_field exists or not.

Is it possible to put multiple get_field’s into one variable and use that variable to check if any of the singular get_field variables exist?

Read More

So for example I have these variables below, and then put them all into one: $promotions

<?php 

    $rider_plan_code        = get_field('rider_plan_code');
    $low_rate_finance_code  = get_field('low_rate_finance_code');
    $custom_promo_code      = get_field('custom_promo_code');

    $promotions = isset( $rider_plan_code, $low_rate_finance_code, $custom_promo_code );

?>

Then I am using this… <?php if ($promotions) { ?> to check if any of those variables exist, but its not working.

Can anyone point out what I’m doing wrong?

Many Thanks


See below how I am using this…

<div class="btn-group">

    <a class="btn " href="#"><i class="icon-share"></i> Post to wall</a>

    <a class="btn <?php if ($promotions) { echo 'dropdown-toggle'; } ?> pricing-btn" href="#" title="Pricing" <?php if ($promotions) { echo 'data-toggle="dropdown"'; } ?>>
        <strong>£<?php the_field('rrp-pound-sterling'); ?></strong><?php if ($promotions) { echo ' <span class="caret"></span>'; } ?>
    </a>

    <?php if ($promotions) { ?>

        <ul class="dropdown-menu">
            <?php if ($rider_plan_code) { ?><li><a href="#">Rider Plan</a></li><?php } ?>
            <?php if ($low_rate_finance_code) { ?><li><a href="#">Low Rate Finance</a></li><?php } ?>
            <?php if ($custom_promo_code) { ?><li><a href="#"><?php echo $custom_promo_name; ?></a></li><?php } ?>
        </ul>

    <?php } ?>

</div>

Related posts

Leave a Reply

3 comments

  1. Are you sure that your ‘get_field()’ function returns NULL when a field is not set? I think isset is giving you true even if the custom field is empty. The vaule has to be NULL in order to get false. Do a var_dump on a single field when its not set.

    var_dump( get_field('rider_plan_code'));
    
  2. From the PHP manual:

    If multiple parameters are supplied then isset() will return TRUE only
    if all of the parameters are set. Evaluation goes from left to right
    and stops as soon as an unset variable is encountered.