<If>with taxonomy / taxonomy field values

Playing around a bit with conversion, and have some observations about the IF tag when working with taxonomies.

Here’s the old code:

[if taxonomy=zoning]
[set autozonetest][taxonomy property-city field=“autozone”][/set]
[-if var=“autozonetest” compare="=" value=“Yes”]
[Autozone Setup]
[/-if]
[/if]

Determining if a taxonomy for the current post has any term associated with the post was pretty easy:

<If taxonomy=zoning post=current>

Setting the variable from the field in the 2nd taxonomy was also pretty straightforward:

<Loop taxonomy=property-city post=current>
  <Set name=autozonetest>
  <Field autozone />
  </Set>
</Loop>

BUT… it would be so much more convenient if you could just do:

<If taxonomy=property-city field=autozone is="Yes" post=current>

Otherwise it would seem like you need to constantly loop, or you would loop once through a taxonomy and declare everything as a variable and then call the variable with SET and GET.

By the way, took me a while to figure out that rather than using compare=“is” you now use is=“Yes” - I might start posting a few generic examples in the forum for people as I’m sure it might reduce the number of posts you have to respond to on a daily :slight_smile:

I think the idea of needing to use loops is that you need to tell WordPress where to look for the data you want to work with. If you use the If tag within a post, L&L is smart enough to know that you probably want WordPress to look through the fields of whatever post you’re currently on, so no need to specify where it should look. On the other hand, if you’re writing some markup on the home page and you want to use the If tag to compare some data from somewhere else on your site, you’ll intuitively know that you need to wrap that If in a Loop so that WordPress knows where to look. I think adding “code shortcuts” as you suggested is sometimes worthwhile, but I think it’s also risky because by creating shortcuts, you can sometimes lose sight of what the code you’re writing is actually doing and, as a result, end up writing bad or inefficient code.

In the example you showed, you’re not actually comparing the fields of the current post, you’re comparing the fields of a taxonomy that happens to be applied to that post. The taxonomy fields aren’t stored within the post itself, so you need to tell WordPress where to look for those taxonomy fields by wrapping your If statement inside a Loop.

I’m not sure exactly how your property-city and zoning taxonomies or terms are set up so this might not be exactly what you’re trying to achieve, but I’m pretty sure this would be the most logical way (to me) of looping through the property-city taxonomy terms and setting a variable if the term’s “autozone” field is “Yes”:

<Loop taxonomy=property-city post=current>
  <If field=autozone is value="Yes">
    <Set name=autozonetest><Field autozone /></Set>
  </If>
</Loop>

Also:

took me a while to figure out that rather than using compare=“is” you now use is=“Yes”

I don’t think that’s right. I’m not very familiar with CCS myself, but the L&L documentation suggests that you can just use the comparison is by itself, but there’s no indication that you can add a value to that comparison. I just tried writing some L&L using is="some_value" and that syntax didn’t work. The syntax that follows the documentation would be is value="some_value".

I hope that’s helpful, let me know if I was totally off-base in my understanding of what you were trying to accomplish :stuck_out_tongue:

Interesting. I took the is= from something I saw in one of Eliot’s posts.

<If field=landarea more_than="{Get rangelow}" less_than_or_equal="{Get rangeone}">

On this post - If with variables - Loops & Logic - Tangible Talk

So just to see what happens - I put the following:

<If check="{Get name=autozonetest /}" is value="Yes">Y1
       <Else />N1
 </If> 
  
<If check="{Get name=autozonetest /}" is="Yes">Y2
       <Else />N2
 </If> 

Output on Page: Y1 Y2

Oh, interesting! I just tested your syntax again and based on what I’m seeing, when you use your syntax for the is comparison, the If statement always ends up being true no matter what.

<Set name=david_rocks>definitely</Set>

<If check="{Get david_rocks}" is value="definitely">Y1 
  <Else />N1 
</If>

<If check="{Get david_rocks}" is value="maybe">Y2 
  <Else />N2 
</If>

<If check="{Get david_rocks}" is="definitely">Y3 
  <Else />N3 
</If>

<If check="{Get david_rocks}" is="maybe">Y4 
  <Else />N4 
</If>

Output: Y1 N2 Y3 Y4

Y1 and N2 are what you’d expect when using the documented syntax, but when you use yours and Eliot’s is="some_value" syntax, the If statement always ends up being true. Maybe Eliot made the more_than and less_than_or_equal comparisons work this way, but not the is comparison? That would be weird, but he’s the only one who could answer that. In any case, this seems to have confirmed that it’s safer to go with what’s in the documentation, which is is value="some_value".

Edit: I just checked out Eliot’s comment in that other thread, it seems he specified that while the syntax could eventually work the way you/he wrote it to allow for comparing more than one value in a single If tag, there currently isn’t support for that syntax. Seems this might be added eventually, but won’t work for now.

1 Like

To check if something is equal to a given value, Ben is right that the expected sytnax is like this:

<If field=autozone is value="Yes">

The default comparison operator is is, so it can be omitted:

<If field=autozone value="Yes">

I wasn’t able to implement this shortcut that you suggested:

<If taxonomy=property-city field=autozone is="Yes" post=current>

The reason why it’s not possible is that a taxonomy can have multiple terms, and this shortcut assumes a single term. Also, the shortcut is not simply checking the term name, but a field value from the term.

I think this is the shortest way to achieve the same behavior as the original CCS shortcodes:

<Taxonomy property-city>
  <If field=autozone value="Yes">
    ..Autozone Setup..
  </If>
</Taxonomy>