2

I have the following code in my template, which displays articles that have been tagged as 'related' through a relationship to the current article being viewed:

{exp:channel:entries channel="blog_entries" require_entry="yes" url_title="{segment_3}"}
{related_articles}

   {if related_articles:count == 1}
      <div  id="related-articles" class="row">
         <div class="container">
            <div class="col span-12"><h3>Related Articles</h3></div>
   {/if}

   <div class="col span-4">
      <h4><a href="/blog/article/{related_articles:url_title}">{related_articles:title}</a></h4>
   </div>

   {if related_articles:count == related_articles:total_count}
      </div>
      </div>
   {/if}

{/related_articles}
{/exp:channel:entries}

This works fine for articles that have a relationship set up. My problem is that on articles that do not have any related articles set up via a relationship, the closing {/related_articles} tag is displayed in the page render. What am I doing wrong here?

EDIT: Here are some screenshots indicating the results I am getting.

Here is a test blog post that has a related article: https://i.stack.imgur.com/HTz35.png

And here is a test blog post that does not have a related article: https://i.stack.imgur.com/QRkv0.png

2 Answers 2

1

Are you sure your last conditional shouldn't be {if related_articles:count == related_articles:total_results}? I can't find mention of what the count and other related variables would be for the relationship field, so I suspect it falls back to the same variable set as the entries loop, in which case total_results would be what you would want here.

Alternatively, I would suggest eliminating all but the title between the {related_articles}{/related_articles} tags just to see if the issue persists and then build it back up. If I had to guess, it may be something off with your conditional (such as what I mentioned above) and that then may in turn prevent the rest of the loop from rendering as expected.

Just to confirm - is related_articles a field in your blog field group?

3
  • Yes, blog_entries has a one-to-many relationship set up to other blog_entries via the 'related_articles' field. I have tried using both total_results and total_count in the final conditional, but they both return the same results.
    – Vortel
    Oct 9, 2013 at 15:20
  • Which version of EE? Just wondering if it may be a reported or unreported bug. It certainly looks like it should work as you have it. Oct 9, 2013 at 19:17
  • We are using EE v2.6.1
    – Vortel
    Oct 9, 2013 at 20:38
0

It looks like the way you've structured your html would easily allow for moving the opening and closing html outside of your Playa loop. Then by wrapping everything in just one conditional to check for relationships you can simplify and eliminate the need to conditionally output html.

Try this instead:

{exp:channel:entries channel="blog_entries" require_entry="yes" url_title="{segment_3}"}

  {if related_articles}
     <div  id="related-articles" class="row">
        <div class="container">
           <div class="col span-12"><h3>Related Articles</h3></div>
              {related_articles}
                 <div class="col span-4">
                    <h4><a href="/blog/article/{related_articles:url_title}">{related_articles:title}</a></h4>
                 </div>
              {/related_articles}
        </div>
     </div>
  {/if}

{/exp:channel:entries}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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