10

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.

17

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}
| improve this answer | |
  • Shouldn't it be ... == 0 instead of == "" ? – BNetz Mar 26 '16 at 0:06
  • This doesn't work in EE 3.5.11. what version does this apply to? – Andy Oct 3 '17 at 11:25
10

Long time to add to this but...

{relationship-field}
  do relationship stuff
  {if relationship-field:no_results}
    Empty stuuf
  {/if}
{/relationship-field}
| improve this answer | |
  • 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. – Laura Montgomery Jun 19 '15 at 13:53
  • Thank you for your answer, Nevsie-Modeten. Your no_results code example really helped me fix my problem. – user7225 Mar 24 '16 at 18:14
  • I love you all... Just what I needed. Thanks. – samc449 Mar 31 '16 at 9:16
0

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}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.