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.

reverse_related_entries

February 18, 2011 4:51am

Subscribe [3]
  • #1 / Feb 18, 2011 4:51am

    jemes

    7 posts

    I have the reverse_related_entries code below that displays a course area title and then displays the course items that are related to that specific course area.

    The code below works but some course areas don’t have any related course items.

    I would like to change the code below to only display the course area title if there are course items related to that area.

    Any help or suggestions of a better way of implementing this would be really helpful.

    {exp:channel:entries channel="course-area" orderby="title" sort="asc"}
        
        <h3><a href="#">{title}</a></h3>
    <p>    <br />
        <div><br />
        <ul class="list_circles"><br />
        {reverse_related_entries orderby="title" sort="asc"}   <br />
        {if course_Type=="Full Time"} <br />
                <li><a href="http://{url_title_path=course}">{title}</a></li><br />
        {/if}<br />
        {/reverse_related_entries}<br />
          </ul><br />
        </div><br />
    {/exp:channel:entries}

  • #2 / Feb 18, 2011 8:59am

    Sue Crocker

    26054 posts

    See: http://ellislab.com/expressionengine/user-guide/modules/channel/reverse_related_entries.html

    In the example, see:

    {if no_reverse_related_entries}
      <h2>No Events for This Performer</h2>
    <p> {/if}

    Does that help?

  • #3 / Feb 18, 2011 9:46am

    James Smith

    259 posts

    Correct me if I’m wrong but I think Sue’s solution only works inside the reverse_related_entries loop, but you need a conditional on the outside of the loop.

    As far as I know there’s no such conditional (feature request?), but you can emulate it with the {count} variable like this:

    {exp:channel:entries channel="course-area" orderby="title" sort="asc"}
        <h3><a href="#">{title}</a></h3><p>    </p>
    
    <p>    {reverse_related_entries orderby="title" sort="asc"}<br />
           {if count == 1}<div><ul class="list_circles">{/if}  <br />
           {if course_Type=="Full Time"} <br />
                <li><a href="http://{url_title_path=course}">{title}</a></li><br />
           {/if}<br />
           {if count == total_results}</ul></div>{/if}<br />
        {/reverse_related_entries}</p>
    
    <p>{/exp:channel:entries}

  • #4 / Feb 18, 2011 9:55am

    James Smith

    259 posts

    PS, for passing the h3 title into the inner loop, I would use the excellent String plugin:

    http://devot-ee.com/add-ons/string/

    http://www.emarketsouth.com/add-ons/string-plugin/examples/#parent-data-in-relationships

    ... or you can also do it for free using an embedded template, passing the relationship ID as an embed variable and then in the embedded template you’ll need to use the query module or some basic php to convert the relationship ID into its relevant entry_id which can then be used in another channel:entries loop. (Basically it’s a right pain, and quite inefficient so I’d go with the String plugin!)

    It’s possible of course that I’ve missed something obvious, and please EL correct me if that’s the case, but these are the only ways I’ve found for achieving this

  • #5 / Feb 18, 2011 10:45am

    jemes

    7 posts

    Thanks for the reply At the Gates.

    Your right the biggest problem is getting the

    Title tag to display only when there are related results. I can get the Course Area ID and then query the Course Area again for the Course Area Title but this doesn’t seem like an effective solution. Would it be possible to get the {title} value and store this in a php variable. Then display that variable only if there are related entries?
    {exp:channel:entries channel="course-area" orderby="title" sort="asc"}
        
    <?php
     $title-value = {title}
    ?>    
    
        {reverse_related_entries orderby="title" sort="asc"}       
           {if course_Type=="Full Time"}
                <h3><a href="#"><?php echo $title-value; ?></a></h3><p> <br />
                <li><a href="http://{url_title_path=course}">{title}</a></li><br />
           {/if}<br />
           </ul></div>{/if}<br />
        {/reverse_related_entries}</p>
    
    <p>{/exp:channel:entries}

  • #6 / Feb 18, 2011 3:24pm

    Ingmar

    29245 posts

    Thanks for stepping in, guys. I’m going to move this thread to the CSC so we don’t have to close it.

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

ExpressionEngine News!

#eecms, #events, #releases