Shortcodes that reference ACF are working

I have created some custom fields using ACF.

I am displaying one of those custom fields inside the content builder using this 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. 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 is the settings page for my ACF field: http://nimb.ws/RFKukJ

Hi There,

Please try with this code instead:

function shortccode_14_eyes() {

	$privacy = get_field('privacy');	

	if ( isset($privacy['fourteen_eyes_country']) && $privacy['fourteen_eyes_country'] == true ):
		return '<span style="color:#EE121E;"><i class="fa fa-exclamation-triangle"></i></span>';
	endif;

}
add_shortcode( '14_eyes', 'shortccode_14_eyes' );

For more information, please take a look at this article: https://www.advancedcustomfields.com/resources/group/.

Regards!

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?

Hey @thecashbag,

This could be a bug with ACF since you also tested it with a third party plugin and the result was the same. I’ll post this to our issue tracker so our development team could forward this to ACF Pro’s developers once they go through the list of reports. If this issue is critical for your project, you will need a separate ACF Pro license so you could open this up to them directly rather than going through us which would take some time.

Thanks.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.