I’ve run into an issue where I need to conditionally show content if an EE relationship field is empty. However the {if relationship-field} conditional is not working. Nor is any variation of that such as {if relationship-field == ""} or {if "{relationship-field}" == ""} or {if "{relationship-field}" != ""}. No dice on any of these.
3 Answers
The solution turns out to be fairly simple and the logic can pretty much work the same. EE relationship fields let you pull the count separately from the tag pair like this:
{relationship-field:count}
So, the solution is to use that in our conditional logic:
{if "{relationship-field:count}" == ""}
do stuff
{if:else}
do relationship stuff
{/if}
Long time to add to this but...
{relationship-field}
do relationship stuff
{if relationship-field:no_results}
Empty stuuf
{/if}
{/relationship-field}
-
Thanks for this. I think this is the most up-to-date solution following changes to EE's conditionals. TJ Draper's code didn't work for me. Your no_results code worked like a charm. Jun 19, 2015 at 13:53
-
Thank you for your answer, Nevsie-Modeten. Your no_results code example really helped me fix my problem.– user7225Mar 24, 2016 at 18:14
-
I would like to add the following also works:
{if "{relationship_field:count}" == 1}
Do relationship stuff
{/if}
In my case, I only want to display formatted content if there is relationship data:
{if "{bk_author:count}"=="1"}
<h5 class="small">by {bk_author backspace="2"}<a href="/author/{bk_author:url_title}">{bk_author:title}</a>, {/bk_author}
</h5>{/if}