ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Check for existence of related entries data

September 24, 2012 11:29am

Subscribe [1]
  • #1 / Sep 24, 2012 11:29am

    aircrash

    293 posts

    I have a template where I need to check whether an entry has related entries data. Normally, I would just use the {if no_related_entries} tag, but in this case, it won’t work.

    If my main entry has a related entry, I need to output the {entry_id} of the related entry. If my main entry has no related entry, I need to output the {entry_id} of the main entry.

    I tried this first, and it didn’t work:

    {related_entries id="problem"}{entry_id}{if no_related_entries}{entry_id}{/if}{/related_entries}

    and I tried this:

    {if problem == ""}{entry_id}{/if}
    {if problem != ""}{related_entries id="problem"}{entry_id}{/related_entries}{/if}

    That didn’t work either. Is there a way to accomplish what I’m trying to do?

  • #2 / Sep 25, 2012 1:23pm

    Kevin Smith

    4784 posts

    Hi aircrash,

    I think I need to get some fuller context for what you’re working on here. Could you provide the full Channel Entries tag you’re working with?

  • #3 / Sep 25, 2012 1:36pm

    aircrash

    293 posts

    Hi Kevin,

    I got it working using the code below, with the full relationship field inside my if statements, but it doesn’t seem like the most efficient method of doing so, since I end up calling the related entries field three times. Is there a better way?

    {exp:channel:entries channel="pages" fixed_order="{embed:entries}" dynamic="no" }
      {if "{related_entries id="problem"}{entry_id}{/related_entries}" == ""}
        <li><a href="http://{path=" title="">{title}</a></li>
      {if:else}
        <li><a href="http://{path=" title="">{title}</a></li>
      {/if}
    {/exp:channel:entries}
  • #4 / Sep 25, 2012 1:46pm

    Kevin Smith

    4784 posts

    Well, the relationship field, once called, isn’t going to add much overhead by being called again in the same tag. I’d be more worried about that embed within the fixed_order parameter value, honestly.

    If the contents of your conditionals are more complex than the simple list item and link shown in your example, you might also want to use simple conditionals instead of an advanced conditional there within the tag, like so:

    {exp:channel:entries channel="pages" fixed_order="{embed:entries}" dynamic="no" }
      {if "{related_entries id="problem"}{entry_id}{/related_entries}" == ""}
        <li><a href="http://{path=" title="">{title}</a></li>
      {/if}
      {if "{related_entries id="problem"}{entry_id}{/related_entries}" != ""}
        <li><a href="http://{path=" title="">{title}</a></li>
      {/if}
    {/exp:channel:entries}

    Overall, I think you’ll be fine here. Are you noticing a load time that’s greater than you’d expect?

  • #5 / Sep 25, 2012 2:19pm

    aircrash

    293 posts

    Thanks, Kevin. Load time hasn’t really been an issue, so I think this will be fine. I think I may even be able to eliminate the embed by using Low Variables’ new preparse tag.

  • #6 / Sep 26, 2012 1:50pm

    Kevin Smith

    4784 posts

    Oh, very nice! Anything else I can help with?

  • #7 / Sep 26, 2012 3:48pm

    aircrash

    293 posts

    No, I’m good now. Thanks!

  • #8 / Sep 27, 2012 11:54am

    Kevin Smith

    4784 posts

    Awesome, have a good one aircrash!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases