1

I'm building what I imagine to be a fairly stock-standard page template on a very standard EE setup.

I'm attempting to use EE 2.6.1's new native Relationships field type to link articles to each other, and to pages from another channel.

What I need to do is check if there are any related articles for the current page/article before I spit out the related content (if it's there). This is mainly so I can show or hide the "Related Articles" as appropriate.

My basic code looks like this:

{if related_articles}
    <h3>Related Articles</h4>
    {related_articles}
        {related_articles:title} (etc...)
    {/related_articles}
{if:else}
    There are no related articles.
{/if}

This just returns the message about there being no related articles.

However, if I take the {related_articles} loop out of the conditional, it performs exactly as expected.

This type of check would work just fine with any other type of channel field variable, so I'm a little confused as to why it's not working here.

I've checked over the official Relationships documentation a couple of times, but it doesn't seem to mention this type of scenario.

Am I doing something wrong, or missing something obvious? Is this something that can be achieved with EE's new native relationships?

Update: Ok, shortly after posting this I found this question, the answer to which helped me out.

I find the solution to be a little messy (nesting my title and any opening and closing DIVs within the loop itself - potentially necessitating multiple conditionals within the loop) but it gets the job done.

1 Answer 1

3

That won't work as the relationship is a tag pair, so at it's basic you could use:

{if "{related_articles}{related_articles:title}{/related_articles}"}
    <h3>Related Articles</h4>
    {related_articles}
        {related_articles:title} (etc...)
    {/related_articles}
{if:else}
    There are no related articles.
{/if}

But that's really clunky.

The documentation isn't great, but reading the comments from the docs, Hop Studios has the solution:

 {parents field="my_field"}
   {if parents:no_results}NOTHING FOUND{/if}
   (<a href="{comment_url_title_auto_path}">{parents:title}</a> {count} {parents:count})
 {/parents}

So at a guess, try this:

{related_articles}
    {if no_results}
        There are no related articles.
    {/if}
    {if count == 1}<h3>Related Articles</h3>{/if}

    {related_articles:title} (etc...)
{/related_articles}

This may also work...

{if related_articles:no_results}
    ...
{if:else}
    ...
{/if}

FYI: Your original... <h3>Related Articles</h4>

4
  • Hi Peter, thanks for your response! I actually like your "clunky" idea better than what I've ended up with (see the link in my update). I find nesting conditionals that will only ever return true once (for example, if count==1) within loops to be far clunkier, if for no other reason than it has to cause a performance hit (however negligible). Aug 27, 2013 at 2:09
  • 1
    Actually, I ust tried your final solution (if related_articles:no_results) and it worked beautifully! Seems that's a pretty ideal way to run it, just requires turning my mental processes around a bit. Thanks! Aug 27, 2013 at 2:17
  • Another followup: that solution generates a parse error if the conditional returns true; that is if I'm on a page that doesn't have any related articles. Guess I'm going back to running the conditional check within the loop. Aug 27, 2013 at 6:05
  • Doh! My else was wrong, not sure if you modified that in your code. Also you may be able to wrap it in quotes - {if "{related_articles:no_results}"} Aug 27, 2013 at 9:14

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.