简体   繁体   中英

Wordpress Custom Fields Checkbox - PHP If Statement

Basically I have some custom fields set up which the admin can complete to show a 'deal' on the homepage. However, there will be times when a deal isn't running, so I've added a checkbox which simply says 'no_deal'.

What I am wanting to do is write an if statement in my page template which displays either the deal (if the checkbox isn't ticked), or a placeholder image (if the checkbox is ticked).

I've been on the Advanced Custom Fields website - http://www.advancedcustomfields.com/resources/field-types/checkbox/ but I'm unsure what to do.Below is what I have attempted but it doesn't work and just breaks the page. Thanks!

<?php if( in_array( 'no_deal', the_field('no_deal') ) { ?>
                                <h4 class="box-heading">NO DEAL</h4>
                            <?php } else { ?>
                                <h4 class="box-heading"><?php the_field( 'deal_title' ); ?></h4>
                                <div class="deal-title"><?php the_field( 'deal_value' ); ?></div>
                                <div class="deal-subtitle"><?php the_field( 'deal_subtitle' ); ?></div>
                                <div class="deal-terms"><?php the_field( 'deal_terms' ); ?></div>
                            <?php }); ?>

Use get_field instead.

From the documentation :

  • get_field : returns an array of values
  • the_field : returns a string containing all values separated by a comma

So, you want something like this:

if( in_array( 'no_deal', get_field('no_deal') )

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM