Home › Forums › Front-end Issues › If Statement Not Working
I have created some custom fields using ACF.
And I am displaying one of those custom fields inside the Theme X content builder using a shortcode I made:
// Add Shortcode
function shortccode_14_eyes() {
if ( get_field( 'privacy_fourteen_eyes_country' ) ):
return '<span style="color:#EE121E;"><i class="fa fa-exclamation-triangle"></i></span>';
endif;
}
add_shortcode( '14_eyes', 'shortccode_14_eyes' );
If the field privacy_fourteen_eyes_country
is true
, it will display a font awesome icon.
The problem is that the icon is displaying even when the custom field has a false
value. The icon shows no matter what value the field has (true or false).
I thought maybe my code was wrong so I tested it using https://wordpress.org/plugins/custom-content-shortcode/ which generates shortcodes for ACF.
[if field=privacy_fourteen_eyes_country value=1]<span style="color: #ee121e;"><i class="fa fa-exclamation-triangle"></i></span>[/if]
But it has the exact same problem. The icon shows no matter what value the field has.
I’m having a really frustrating and wasteful day trying to get this working. Do you know what is going wrong?
Here are the settings page for my ACF field: http://nimb.ws/RFKukJ
As per another thread (https://support.advancedcustomfields.com/forums/topic/the-group-field/) I have also tried the below code, but it doesn’t work either.
/* Add 14 eyes shortcode */
function shortcode_14_eyes() {
$privacy = get_field('privacy');
if( $privacy ):
if( $privacy['fourteen_eyes_country'] ):
return '<span style="color:#EE121E;"><i class="fa fa-exclamation-triangle"></i></span>';
endif;
endif;
}
add_shortcode( '14_eyes', 'shortcode_14_eyes' );
UPDATE: I can get things working, but then as soon as I change one of the custom fields on the backend it stops working. It will just display nothing, or display an old value.
I have no caching plugins running, and cloudflare is turned off. I am also clearing the browser cache each time.
Why is this happening?
The topic ‘If Statement Not Working’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.