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.

Putting variables in links, and related_entries ignoring categories.

October 17, 2012 12:53pm

Subscribe [2]
  • #1 / Oct 17, 2012 12:53pm

    Finn

    40 posts

    Hi,

    My heirachy is:

    Exhibitions
    -Artists
    —Works

    To achieve this I’ve made Artists and Works channels, with Artists a parent of Artists. I’ve then made Exhibitions a category.

    I’ve applied Exhibitions categories to various Artists and Works.

    I have three pages:

    Exhibitions-List page.

    exp:channel:categories channel="exhibitions" style="linear"}
    
        <a href="http://{path=exhibitions/index}">{category_name}</a>
    
    {/exp:channel:categories}

    Exhibitions page.

    {exp:channel:entries channel="artists" sort="asc"}
    
     <h1>{title}</h1>
    
      {reverse_related_entries orderby="title" limit="1"}
    
       <div><a href="http://{path=artists/C1/index}">{work_image:thumb}</a></div>
    
      {/reverse_related_entries}
    
    {/exp:channel:entries}

    And an Artists page:

    {exp:channel:entries channel="artists" sort="asc"}
    
     <h1><a href="http://{title}">{title}</a></h1>
    
      {reverse_related_entries orderby="title"}
    
       <div><a href="http://{work_image}">{work_image:thumb}</a>
    
       {work_caption}</div>
    
      {/reverse_related_entries}
    
    {/exp:channel:entries}

    I have two questions:

    1. For the Exhibitions page, how do I make the category within the link take it’s value from the category in the current page’s URL?

    2. For the Artists page, it is correctly only displaying Artist entries that have the category showing in the URL (e.g. /artists/C1/). However the Work entries that are in the reverse_related_entries section are ignoring the category and so all Works are being shown, regardless of their category.

    Thanks!

  • #2 / Oct 18, 2012 3:03pm

    Dan Decker

    7338 posts

    Hi Finn,

    Excellent code samples! Thanks!

    For item 1, use the {active} conditional:

    <a href="http://{path=%27news/index%27}" class="active">{category_name}</a>

    On number 2, try a conditional in the reverse related pair:

    exp:channel:entries channel="artists" sort="asc"}
    
     <h1><a href="http://{title}">{title}</a></h1>
      
      {reverse_related_entries orderby="title"}
      {categories}
       {if category_id == 1}
       <div><a href="http://{work_image}">{work_image:thumb}</a>
    
       {work_caption}</div>
       {/if}
      {/categories}
      {/reverse_related_entries}
    
    {/exp:channel:entries}

    Let me know what you think!

    Cheers,

  • #3 / Oct 18, 2012 3:48pm

    Finn

    40 posts

    Hi Dan, thanks for your help.

    Hi Finn,

    Excellent code samples! Thanks!

    I’m glad I’ve done something right!

    Hi Finn,

    For item 1, use the {active} conditional:

    <a href="http://{path=%27news/index%27}" class="active">{category_name}</a>

     

    Unfortunately that doesn’t seem to have any effect for some reason.

    On number 2, try a conditional in the reverse related pair:

    exp:channel:entries channel="artists" sort="asc"}
    
     <h1><a href="http://{title}">{title}</a></h1>
      
      {reverse_related_entries orderby="title"}
      {categories}
       {if category_id == 1}
       <div><a href="http://{work_image}">{work_image:thumb}</a>
    
       {work_caption}</div>
       {/if}
      {/categories}
      {/reverse_related_entries}
    
    {/exp:channel:entries}

     

    Sorry, I should have been clearer in my second question. The category is going to vary since it is coming from the previous page, which in turn has come from the list on the first page.

    What I’m trying to do is have three pages, the first of which lists the categories, e.g.

    -Exhibition 1
    -Exhibition 2
    -Exhibition 3

    Then when you click an entry it takes you to the second page who’s url is /exhibitions/C1/ (or C2 etc.). I’ve managed that bit alright.

    The second page then lists Artist names and a single Work image for each Artist (both the Artist names and Work images must belong to that particular Exhibition). This is where I need the class=“active” thing that unfortunately doesn’t seem to be working.

    -Artist 1
    —Work 1
    -Artist 2
    —Work 2
    -Artist 3
    —Work 3

    Then when you click an Artist name or Work image, it takes you to the third page, which displays a single Artist name and multiple Work images (the images must again belong to that particular Exhibition). This is where I need the reverse_related_entries to respect the /c1/ or /c2/ etc. in the URL (assuming that I’ve managed to get it in the URL in the first place).

    -Artist 1
    —Work 1
    —Work 2
    —Work 3
    —Work 4

    One other thing that I failed to notice, is that I’m also failing to carry the Artist ID/Name from the second to third page, which I will need to do so that the page displays only one Artist, not all.

    I hope all that makes some kind of sense..

  • #4 / Oct 19, 2012 4:39pm

    Dan Decker

    7338 posts

    Hi Finn,

    Ah, well, we can make that dynamic with Seg2Cat.

    {exp:channel:entries channel="artists" sort="asc"}
    
     <h1><a href="http://{title}">{title}</a></h1>
      
      {reverse_related_entries orderby="title" category="{segment_category_ids}"} {!-- or {segment_2_category_id} --}
       <div><a href="http://{work_image}">{work_image:thumb}</a>
       {work_caption}</div>
      {/reverse_related_entries}
    
    {/exp:channel:entries}

    Let me know what you think!

    Cheers,

  • #5 / Oct 19, 2012 4:55pm

    Finn

    40 posts

    Thanks Dan, I’ll try that now. I also need to get the Artist ID, and Artist is a Channel. Is there a Seg2Entry or similar function built in?

    Finn.

  • #6 / Oct 20, 2012 4:41am

    Finn

    40 posts

    This doesn’t seem to do anything. And if I visit the page without a category segment in the url, it erroneously prints “} “} “} on the page.

  • #7 / Oct 23, 2012 10:34am

    Dan Decker

    7338 posts

    Hi Finn,

    I just noticed your note that {active} wasn’t working. Sorry I missed that. It’s a relatively new variable, so you may not be on a version that supports that. What version of ExpressionEngine are you using?

    I also need to get the Artist ID, and Artist is a Channel. Is there a Seg2Entry or similar function built in?

    Excellent question. In a manner of speaking, yes. You can build a URI to hold any information you need and then access that using {segment_n} variables. For example:

    {path='template-group/template/category/painting/artists/joe_painter'}

    In that example, segments 5 & 6 would be “artists” and “joe_painter” and you could call that into your template like so:

    {exp:channel:entries channel="{segment_5}" url_title="{segment_6}" dynamic="no"}

    Where that would pull Joe Painter’s entry from the artists channel based on what’s in the URL. BTW, “joe_painter” could just as easily be an entry ID - entry_id=”{segment_6}”
    ~

  • #8 / Oct 23, 2012 12:34pm

    Finn

    40 posts

    Hi Dan, thanks for the reply.

    Hi Finn,

    I just noticed your note that {active} wasn’t working. Sorry I missed that. It’s a relatively new variable, so you may not be on a version that supports that. What version of ExpressionEngine are you using?
    ~

    I’m using whatever was current a week or two ago. But actually having looked again, I think I was less than clear with my first question in the first post. The question was actually referring to the 2nd block of code I gave, not the first.

    But anyway it’s a moot point as somehow I’ve managed to get it all (nearly) working! Your info on segments has proved particularly useful - thanks! And even better, all with no plugins, nor me attacking my screen.

    I’ve also found switching the URLs from CX to category/category_name has helped, and also enabling strict URLs (perhaps the latter could be the default for beginners to avoid confusion?)

    Page 1/3 now looks like this:

    /exhibitions-list/

    {exp:channel:categories style="linear"}
    
        <a href="http://{path=exhibitions/index/category/{category_url_title}}">{category_name}</a>
        
    {/exp:channel:categories}

    Page 2/3 now looks like this:

    /exhibitions/category/exhibition-X

    {exp:channel:entries channel="artists" sort="asc" }
    
     <h1><a href="http://{path=artists/{segment_2}/{segment_3}/{url_title}}">{title}</a></h1>
    
        {reverse_related_entries orderby="title" limit="1"}
    
       {categories}
    
          {if category_url_title == segment_3}
    
               <div>{work_image:thumb}</div>
    
        {/if}
    
       {/categories}
     
       {/reverse_related_entries}
    
     {/exp:channel:entries}

    And page 3/3 now looks like this:

    /artists/category/exhibition-X/artist-X

    {exp:channel:entries channel="artists" url_title="{segment_4}"} 
    
     <h1>{title}</h1>
    
      {reverse_related_entries}
       
       {categories}
    
          {if category_url_title == segment_3}
          
         <div><a href="http://{work_image}">{work_image:thumb}</a>
    
         {work_caption}</div>
    
           {/if}
      
       {/categories}
    
      {/reverse_related_entries}
    
    {/exp:channel:entries}

    It all works great - except for one thing:

    On page 2/3 the reverse_related_entries section was correctly displaying only those Works that had the current Exhibition category applied to them. But it was displaying multiple Works per Artist, when I only wanted it to show one for each. So to solve this I added limit=“1” to the reverse_related_entries tag, but it gives me an unexpected result:

    It feels like when I apply limit=“1”, the reverse_related_entries tag first finds all the Works that have ANY Exhibition categories assigned to them (or perhaps more likely just all Works regardless of category). It then applies a limit of 1 to the results i.e. selects one image per artist. Only after it has done all that does it check to see which specific Exhibition category is in the URL. If the current URL category coincides with the category that is assigned to the Work that the tag happened to select, it displays it - if the category doesn’t match, it doesn’t.

    Is that comprehensible at all? How can I get around this?

  • #9 / Oct 24, 2012 12:19pm

    Dan Decker

    7338 posts

    Hi FInn,

    I love seeing progress!

    On your last exception, indeed. you need to get the category parameter into the reverse_related tag along with limit.

    They way you have it, limit=“1” is going to pull the latest entry regardless of category. If you get Seg2Cat, you can still make use of the {segment_3_category_id} variable, even though you are using category_url_title - Seg2Cat will do the work of making that an ID, so you would go with:

    {exp:channel:entries channel="artists" sort="asc" }
    
     <h1><a href="http://{path=artists/{segment_2}/{segment_3}/{url_title}}">{title}</a></h1>
    
        {reverse_related_entries orderby="title" limit="1" category="{segment_3_category_id}"}
    
       {categories}
               <div>{work_image:thumb}</div>
       {/categories}
     
       {/reverse_related_entries}
    
     {/exp:channel:entries}

    I applaud the desire to be plugin free, but ExpressionEngine is just limited in the way it can pull category info on it’s own. Low’s add-ons are top-notch and worry-free. Seg2Cat is part of my default build.

    Cheers,

  • #10 / Oct 24, 2012 12:51pm

    Finn

    40 posts

    Hi Dan,

    I love seeing progress!

     

    Me too!

    On your last exception, indeed. you need to get the category parameter into the reverse_related tag along with limit.

    They way you have it, limit=“1” is going to pull the latest entry regardless of category. If you get Seg2Cat, you can still make use of the {segment_3_category_id} variable, even though you are using category_url_title - Seg2Cat will do the work of making that an ID, so you would go with:

    {exp:channel:entries channel="artists" sort="asc" }
    
     <h1><a href="http://{path=artists/{segment_2}/{segment_3}/{url_title}}">{title}</a></h1>
    
        {reverse_related_entries orderby="title" limit="1" category="{segment_3_category_id}"}
    
       {categories}
               <div>{work_image:thumb}</div>
       {/categories}
     
       {/reverse_related_entries}
    
     {/exp:channel:entries}

    I applaud the desire to be plugin free, but ExpressionEngine is just limited in the way it can pull category info on it’s own. Low’s add-ons are top-notch and worry-free. Seg2Cat is part of my default build.

    Cheers,

    I previously tried Seg2Cat and it didn’t do anything at all for some reason (I think I mentioned that somewhere above).

    I’ve just tried it again with your suggested code, and while it is now doing something at least, it’s still not right.

    It’s doing nearly the same as I had yesterday using the IF statement, only it outputs the image regardless of whether it matches the current category in the URL or not.

    i.e. It is simply picking the first Work that it can find for each Artist, and then outputting it. It’s not bothering to check that those Works match the current category in the URL.

    edit: I’ve just read a suggestion that the Category tag isn’t supported in a reverse related entry?

  • #11 / Oct 25, 2012 3:04pm

    Dan Decker

    7338 posts

    Finn,

    You may be right. I found this bug report, and Robin has a note in there to work around it.

    I find it odd that the report was for 2.2.2 and the bug was never “Accepted”, but the fix is in the comments.

    Can you give that a look and let me know your result?

    Cheers,

  • #12 / Oct 25, 2012 3:31pm

    Finn

    40 posts

    Hi Dan,

    I’ve tried the fix and unfortunately it isn’t making any difference…

  • #13 / Oct 29, 2012 4:02pm

    Kevin Smith

    4784 posts

    Hi Finn,

    Could you clarify for me? Is this the bug fix you tried? I know you’ve worked through a lot of things with Dan here, so could you show the template code you’re using now that’s causing this last remaining issue?

  • #14 / Oct 29, 2012 4:11pm

    Finn

    40 posts

    Hi Kevin, thanks for the reply.

    Yes that’s the bug fix I tried.

    The code I tried is the same as above:

    {exp:channel:entries channel="artists" sort="asc" }
    
     <h1><a href="http://{path=artists/{segment_2}/{segment_3}/{url_title}}">{title}</a></h1>
    
        {reverse_related_entries orderby="title" limit="1" category="{segment_3_category_id}"}
    
            {categories}
    
               <div>{work_image:thumb}</div>
    
            {/categories}
     
       {/reverse_related_entries}
    
     {/exp:channel:entries}

    I subsequently got in touch with the creator of Seg2Cat, and he pointed out that the EE documentation suggests that the reverse_related_entries variable pair doesn’t support the category=”” parameter. I don’t know if that’s the answer or not…

  • #15 / Oct 29, 2012 4:23pm

    Kevin Smith

    4784 posts

    Ok, now I’m up to speed. Yes indeed, category isn’t one of the parameters for that tag, all of which are outlined in the documentation. Just so I can better conceive of the problem, could you give me some examples of an artist that would show up as a result with this tag and then the reverse related entries that you would and would not want to be returned based on their category?

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

ExpressionEngine News!

#eecms, #events, #releases