Hi there, could you please advise if I can use an If statement to check data from the ACF options page? The docs do not explicitly state this is enabled, but it would be useful for enabling features (template logic) from the ACF options page.
Example code:
<If check="{acf_true_false=enable_some_feature from=options}" is value="True">
<p>Field returned true</p>
<Else />
<p>Field returned false</p>
</If>
Thanks, but I’m still having trouble getting this to work.
I can retrieve the options field using Set & Get, but I can’t seem to use it inside an If statement. Maybe I’m doing something wrong, or maybe it’s a bug.
Worth testing, but I believe the ACF True False field returns 1 if true and empty if false. Try outputting the value onto the page to be sure what you should be testing for
I think contains is your issue here, that’s not a valid comparison. Maybe includes or even just is could work. Although is is the default so I would have assumed it would default to that if you use a non-existent comparison, but I’m not sure.
It seems no one has mentioned this in the thread yet, but I think the only reason the syntax in your original post didn’t work is that you were just missing a Field tag in check="{Field acf_true_false=enable_some_feature from=options}". Seems you’ve figured that out based on your reply but I thought I’d mention that for anyone reading this thread in the future.
Edit: I was wrong, see Ralf’s schooling of me below @PolarRacing, you might want to read up on how the If tag works. Attributes are tag-specific so you can’t necessarily place attributes from one tag onto another as you’ve suggested. It’s just like HTML. For example, you couldn’t write <img href="www.site.com" /> because the img tag doesn’t know what to do with an href attribute since that attribute is only designed to work with the a tag.
Yes… Yes, it is! Haha my bad, I’ve seen so many people mix and match attributes I thought that’s what was going on here but I guess I just haven’t played around with ACF’s true/false field to know there was a specific feature there that made it compatible with the If tag. Thanks for sharing the helpful suggestion above and for teaching me something new!
Does anyone actually have an If statement working using from=options? If not, I would say there’s a bug with the logic because I’ve tried many different approaches with no luck.
For reference:
<Field xxx from=options /> returns 1 or 0
<Field acf_true_false=xxx from=options /> returns TRUE or FALSE
The above tags work fine, but using <If...> and from=options always breaks no matter how I write the syntax. E.g. <If field="xxx" from="options" is value="1">
I also tested with an ACF text field and could not get it to work, so this may be a general bug with <If...> and from=options.
As suggested, the workaround is to set the ACF value with a local that can be seen by the If statement. This logic definitely works:
<Set local=acf_check_value>
<Field xxx from=options />
</Set>
<If check="{Get local=acf_check_value}" is value="1">
<p>Field returned 1</p>
<Else />
<p>Field returned 0</p>
</If>
As mentioned by @benjamin - this is a special <If> for acf_true_false - there is no value available. You just get a TRUE if the field is set or a FALSE if not.
This way it will say True or False depending on your field. A way to test this is by simply using the <Field acf_true_false=my_choice_option from=options /> to see what it outputs, which is TRUE for having the choice checked and displaying nothing for having the choice not checked.
This does seem to work, but I don’t think this would be the right approach. Intuitively, that conditional statement would be true if the field exists (exists is the default comparison when no comparison or value are stated), not necessarily if it’s true. However, it does seem to work, but maybe only because when the true/false field is unchecked, it seems to act like the field doesn’t exist at all.
The fact that the syntax as noted in the docs doesn’t work shows that there’s a bug here that needs to be fixed. I think we already had a dev who spied on this thread and started looking into it so someone from the team will follow up here when this has been addressed.
Edit: some more testing revealed that this issue doesn’t even seem to pertain to true/false fields on an options page. It seems that even true/false ACF fields placed on a post/page aren’t reacting as expected based on the syntax noted in the docs. I’ve got a dev looking into this.
Thanks for the update, can they please also check that from=options works with If statements as expected because there could be more than one bug here.