2

I am working with three Relationship fields and I would like to be able to display a section only if there are any related articles in that section.

I tried this without success:

{if related_tips != ""}
  <h3>Related Tips</h3>
   <ul>
    {related_tips}
      <li><a href="{site_url}in_the_garden/decorating_tip/{related_tips:url_title}">{related_tips:title}</a> </li>
    {/related_tips}
   </ul>                
{/if}

I would appreciate some guidance on how this should be done.

Thanks.

1
  • Try removing the != '' part of your equasion – stuartmcd69 Mar 7 '14 at 6:30
4

Try using the count and total results variables, but namespaced with the name of your relationship field:

{relationship_field}

    {if relationship_field:count ==1}
        <ol>
    {/if}

    <li>{relationship_field:title}</li>

    {if relationship_field:count == relationship_field:total_results}
        </ol>
    {/if}

{/relationship_field}
2
  • Hi Michael, That appears to solve the problem. I am not sure I totally understand the logic behind the conditionals around the <ol>. If you could share a tad more on that, I would appreciate it. Also, I would like to thank you for your amazing tutorials. It was your Small Business site tutorial that I went through in 2008 that turned the EE lightbulb on in my mind. Since then I have built more than 50 EE sites and will always be grateful for your teaching. – forrest Mar 10 '14 at 14:23
  • Basically this logic is saying "only spit out the ordered list markup if there are going to be items in the list". It's just a way to ensure that you would never get empty <ol></ol> markup in your rendered code. And thanks for the comments - always good to hear as I look at another necessary edit of the book..;) – Michael Boyink Mar 10 '14 at 17:59

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.