If statement using ACF options page field

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. :slight_smile:

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.

1 Like

Yes you can.

This should do the trick.

<If acf_true_false=field_name from=otions>
...

If not, set a variable with the options field value and use If variable=…

1 Like

It’s easy to miss, but this is documented at the very bottom of the ACF integration documentation page

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.

Current test logic:

<Set local=acf_check>
  <Field acf_true_false=enable_some_feature from=options />
</Set>

<Get local="acf_check" />

<If check="{Get local=acf_check}" contains value="TRUE">
   <p>Field returned true</p>
  <Else />
   <p>Field returned false</p>
</If>

Output:
image

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 :face_with_open_eyes_and_hand_over_mouth: @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.

@benjamin, thats the code from the documentation

<If acf_true_false=field_name>
  TRUE
<Else />
  FALSE
</If>

so it should work with from=options as well. Or do I miss something. (Not using acf_true_false that much)

2 Likes

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!

No issue - I was just wondering if I missed something.

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. :slight_smile:

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>

You have two options:

<If acf_true_false=field_name from=options>
<p>Field returned TRUE</p>
<Else />
<p>Field returned FALSE</p>
</If>

or if you want to use a variable :

<If acf_true_false=field_name from=options>
  <Set my_var>TRUE</Set>
<Else />
  <Set my_var>FALSE</Set>
</If>

<If variable=my_var value=TRUE>
<p>Field returned TRUE</p>
<Else />
<p>Field returned FALSE</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.

Yeah, I know how it works. I’ve been messing around with this logic for hours now.

If you have <If acf_true_false=field_name from=options> working in a live environment right now, I will rescind my claim that there is a bug. :slight_smile:

I just played around with the acf_true_false and you are right. It does not work.

But the code below works. (At least for me)

<Set this><Field test from=options /></Set>
<If variable=this value=1 >
  TRUE
  <Else />
  False
</If>
2 Likes

I have tried to replicate this and the way this could work is using this markup:

<If check="{Field acf_true_false=my_choice_option from=options}">
  TRUE
  <Else />
  False
</If>

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.

2 Likes

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.

1 Like