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.

Related items If statement?

May 09, 2011 1:10pm

Subscribe [2]
  • #1 / May 09, 2011 1:10pm

    dodgson

    120 posts

    I’m new to this stuff, so please bare with me. I’m trying to have a list of items that are in the same related category. If there isn’t a related item, I would like the message to say “We’re sorry there aren’t any related products.”. How would I go about writing this. Thanks in advance.

    {exp:channel:entries channel="products" related_categories_mode="yes"}            
        <ul>
        {if}
        <li><a href="http://{title_permalink=">{title}</a></li>
        {/if}
                        
        {if:elseif}
            We're sorry there aren't any related products.
        {/if}
        </ul>
    {/exp:channel:entries}
  • #2 / May 09, 2011 2:11pm

    Mark Bowen

    12637 posts

    The documentation is your friend on this one 😉

    {if no_related_entries} Conditional

    The {if no_related_entries} conditional allows you specify certain content to be displayed when an entry does not have a related entry for the field specified.

    {exp:channel:entries channel="news" limit="15"}
    
    <h3>{title}</h3>
    
    <p>{body}</p>
    
    <p>{related_entries id="performers"}</p>
    
    <p>         {if no_related_entries}</p>
    
    <p>          </p><h2>No Performer Information Available</h2>
    
    <p>         {/if}</p>
    
    <p>        </p><h2>{title}</h2>
    
    <p>         {body}<br />
    {/related_entries}</p>
    
    <p>Submitted on: {entry_date format='%M %d, %Y'}</p>
    
    <p>{/exp:channel:entries}

    Hope that helps a bit.

    Best wishes,

    Mark

  • #3 / May 09, 2011 2:50pm

    dodgson

    120 posts

    Hi Mark, Thanks again for responding (do you ever sleep?). I’m confused about the following note in the documentation… “Note: The id parameter must contain the Field Name of the custom field that contains the relationship. For example, if your custom field is called performers you’ll use this:”.

    The relationship is being created by selecting a category for each post. Is this the incorrect way of doing this? I just don’t understand what the ID=”” would be.

    Thanks in advance.

  • #4 / May 09, 2011 2:55pm

    Mark Bowen

    12637 posts

    Hi Mark, Thanks again for responding (do you ever sleep?).

    Nah I’m like Gustav Graves from the Bond film except I’m a goodie instead of a baddie!! 😊

    Reading back on what you’re doing I may have jumped the gun slightly on something as you had mentioned related however I’m now wondering if you are actually using relations in your entries or were you just meaning if there are any other entries in the same category?

    Is this going to be used on a single entry page and then you want to check if there are any more entries in the same category as the entry you are viewing perhaps?

    Best wishes,

    Mark

  • #5 / May 09, 2011 3:03pm

    dodgson

    120 posts

    LOL. To be honest, I didn’t really realize there was a difference between creating the categories and using relations in entries. I thought that by selecting a category your were creating a relationship. Sadly still confused. I have been creating single entries and selecting a category (ie. product brand & product type). then I’m trying to create a list where it shows all other products that have the same categories selected. however… If there isn’t another product that has related items, I would like to show a message that says “We’re sorry but there aren’t any related products”.

    I apologize for not being clear. I’m really trying to figure all this stuff out.

    Thanks again for all of your help.
    Mark

  • #6 / May 09, 2011 3:04pm

    Mark Bowen

    12637 posts

    Oops! Sorry!

    I just saw above that you did indeed mention that the relationship is being made via categories. Sorry about that.

    One way to do this could be to use a second Channel Entries Tag and use the related_categories_mode=“yes” parameter in the second tag to only bring back any related entries and then in that tag just use a simple {if no_results} tag to show that message if there indeed aren’t any related entries.

    Best wishes,

    Mark

  • #7 / May 09, 2011 3:07pm

    Mark Bowen

    12637 posts

    LOL. To be honest, I didn’t really realize there was a difference between creating the categories and using relations in entries. I thought that by selecting a category your were creating a relationship.

    You are indeed creating a relationship yes. Sorry it was because I read the title of the thread and got mistaken into thinking that you meant you were using related items. That was my mistake though as I should have read on to see exactly what it was that you were trying to do.

    I apologize for not being clear. I’m really trying to figure all this stuff out.

    No apologies needed at all. I should be the one apologising for not reading carefully enough in the first place 😉

    Best wishes,

    Mark

  • #8 / May 09, 2011 3:08pm

    dodgson

    120 posts

    That did it. Thanks again for your help. It’s very much appreciated.

  • #9 / May 09, 2011 3:17pm

    Mark Bowen

    12637 posts

    That did it. Thanks again for your help. It’s very much appreciated.

    Excellent news. Glad that helped.

    Hope everything else goes well for you.

    Best wishes,

    Mark

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

ExpressionEngine News!

#eecms, #events, #releases