ACF Text Comparison Statements Not Working

My end goal is to conditionally display a heading. To do this, I’m trying to create logic to validate against the value of ACF fields, but no matter what I try, it fails to work properly. In other cases I’m using if statements to see if a field has a value using the “check” method, which does seem to work, but nothing with value=“Some Text”.

I have tried some simple If statements, If-Else, If-EsleIf-Else, and Switch, all with the same results. I don’t care what method I use, as long as it works.

Below is the code that I currently have in my template, and at the end are 3 different links that would would satisfy each of the conditions of the switch statement. I have also tried using local variables vs. direct ACF variables, but get the same results, as you will see code for both options attached as well.

<Set local=service2_type><Shortcode>[acf field="funeral-service-type-2"]</Shortcode></Set>
<Set local=service2_other><Shortcode>[acf field="funeral-service-other-2"]</Shortcode></Set>
<p>
local type= "<Get local=service2_type />"
<br />
local other= "<Get local=service2_other />"
</p>
<If Field="service-type-2" is value="No Service">
  <p style="color:#f00;"> - Local - No Service If Statement -</p>
</If>
<Switch Field="service-type-2">
  <When value="No Service" />
    <p style="color:#f00;">- Local - No Service Switch Statement -</p><br />
  <When value="Other" />
    <h4><Get service2_other /></h4> 
  <When />
    <h4><Get service2_type /></h4>
</Switch>
<br />
<hr style="border-bottom: 2px dashed #f00;" />
<br />
<p>
acf type= "<Field funeral-service-type-2 />"
<br />
acf other= "<Field funeral-service-other-2 />"
</p>
<If Field="funeral-service-type-2" is value="No Service">
  <p style="color:#f00;"> - ACF - No Service If Statement -</p>
</If>
<Switch Field="funeral-service-type-2">
  <When value="No Service" />
    <p style="color:#f00;">- ACF - No Service Switch Statement -</p><br />
  <When value="Other" />
    <h4><Get service2_other /></h4> 
  <When />
    <h4><Get service2_type /></h4>
</Switch>

The links below are provided as example pages for each of the 3 conditions. Results show in the grey box in the right sidebar of the page.

Value = “No Service”

Value = “Other”

  • /joseph-f-big-joe-guilfoile-jr/

Value = Any other condition.

  • /martha-bane/

So, as a new user, the system limiting the number of links I can add, so I’m removing the domain reference from the last 2 links.

Hi Brice :slight_smile:

I’m seeing issues with your syntax that would explain the issues you’re having! Here’s what I would suggest doing (I’m assuming your ACF field is a simple text field):

<Switch field=funeral-service-type-2>
  <When value="No Service" />
   No Service
  <When value="Other" />
    <h4><Field funeral-service-other-2 /></h4> 
  <When />
    <h4><Field funeral-service-type-2 /></h4>
</Switch>

which could also be written as:

<If field=funeral-service-type-2 value="No Service">
   No Service
  <Else if field=funeral-service-type-2 value="Other" />
    <h4><Field funeral-service-other-2 /></h4> 
  <Else />
    <h4><Field funeral-service-type-2 /></h4>
</Switch>

When testing the value of saved variables you can use variable or check, but not field:

<Set my-variable><Field funeral-service-other-2 /></Set>
<If variable=my-variable value="No Service">...
<Set my-variable><Field funeral-service-other-2 /></Set>
<If check="{Get my-variable}" value="No Service">...

Capitalization pretty much only happens with tag names to ensure compatibility with HTML as the language is extended. I imagine that capitalizing the field argument in your <If> tag is what sent you down this rabbit-hole!

1 Like

Julia,

Thank you for the quick response! So my only issue was the fact that I was capitalizing the word “Field” in my if statement!? That was a super simple solution to something I’ve been embarrassingly banging my head against for the last several days…

Updated and simplified code is working like a dream!

<If field=funeral-service-type-2 value="No Service">
    <!---No Service--->
  <Else if field=funeral-service-type-2 value="Other" />
    <h4><Field funeral-service-other-2 /></h4> 
  <Else />
    <h4><Field funeral-service-type-2 /></h4>
</If>

This is my first experience with this plugin, but I can tell you I will be using it every chance I get moving forward! Great service as well!!

2 Likes

Glad to hear you’re loving the plugin! If you’re interested in helping support our team’s development and support surrounding this free plugin, posting a review of L&L on the WordPress repo would go a long way. You’ve basically written the review already!

Whether you feel like doing that or not, I’m happy to hear that you’re planning on getting a lot of use out of it. See you around the forum!

1 Like