Use of conditional on ACF Relationship Field outside of loop

Right. Your current template checks whether there are two items in the loop, it’s not seeing how many items there are that match or don’t match a specific value.

I see two approaches you could take. The first is kinda cool and good to know, but is probably overkill in your case. It would be to use variables with the Set and Get tags. For example, you could set a variable equal to zero at the beginning of your template and then between the opening and closing If tags for your “is_not ‘core’” logic, you could use the Get tag and Math tag to increment the variable (set the variable to be equal to the variable + 1). Then at the end instead of checking the previous_total in your If tag as you’re currently doing, you could check variable=your_variable_name_here.

But that’s probably a bit much. A better way to do this would probably just be to have two loops instead of one. Your first loop could loop through items where the title field is “core” and the second could loop through items where the title isn’t “core.” You might want to read up on filtering the loop to get an idea of the different ways that you can filter your loop. In your case, you’d probably add the following parameters to your “is_not ‘core’” loop: <Loop acf_relationship=plan_or_add-on custom_field=title custom_field_compare=not custom_field_value=Core>. Then you could just leave your If previous_total tag in place since it’ll check the loop count total of the second loop.

Let me know if this helps and if you get that all working as you need it!

There are a couple of different tools built into L&L for this. In your case, I think you’re just going to want to have your value be a comma-separated list, like value="core,basic". Keep in mind that you might want to check whether things are case-sensitive based on your setup. There are lots of different comparisons available on the If tag so if you skim that list, you’ll notice that since your value is a list, you’ll probably want to use not_in.

While that’s probably your best bet in your case, the other tools for getting granular with your If tag would be the Else tag and, if ever you need to stack a bunch of logic with and/or logic, the logic variable might come in handy.

Feel free to share your final template here since it might be helpful to others! Also, if you’re finding this plugin useful or if you’re enjoying the support, you might consider posting a review of the plugin to help share the word. I think the current average rating is 4.8 stars so we’re hoping to get that up to a nice 5.0 average rating :slight_smile:

Whether you feel like doing that or not, keep your questions coming!

1 Like