Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

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>&#163;<?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>
share|improve this question
add comment

3 Answers

ACF does not return NULL variables, it will return an empty. Try checking with !empty instead of isset.

share|improve this answer
add comment

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'));
share|improve this answer
    
this is likely the issue, false is a value. the other potential issue- it sounds like op wants to check if any of them are set, passing multiple vars to isset will only return true if all of them are set. –  Milo Dec 5 '12 at 16:27
    
get_field() will return false if it doesn't exist –  Zach Dec 5 '12 at 17:21
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.