3

On our Single entry pages we recommend other articles. Four slots are available. channel: articles. channel fields: Content Elements field for the text and a native EE Relationship field called "related_articles"

Here´s the editorial workflow: After writing the article an editor can select between 0 and 4 articles manually. The EE-field "related_articles" will be used for that. All relationships are between entries of the same channel.

There are 4 slots to fill. EE will fill the empty slots automatically with articles from the same category.

An example: The editor wrote an article in the category A and manually added one related article. Then EE has to add three more category A articles to the mix.

This is my code for the editor´s picks:

{exp:channel:entries channel="article"}
    {related_articles}
       <p>{related_articles:title}</p>
    {/related_articles}
{/exp:channel:entries}

This is my code for the stuff, that EE adds automatically.

{exp:channel:entries channel='article' 
    related_categories_mode='yes' 
    custom_fields='yes' 
    limit='{4 - count_of_editors_pick}' 
    entry_id='not {the_IDs_from_editors_pick}'
}
  <p>{title}</p>
{/exp:channel:entries}

Both work well solo, but how do I combine them? I tried the math plugin plus the session variables plugin. I stored the {count} for the first loop in {rel_art}, so I know, how many articles the editor added.

{exp:math formula='4 - [1]' params='{exp:session_variables:get name="rel_art"}'}

But it seems, that I run into a parse order issue here. I can´t use this to set the limit parameter within the channel entries tag.

My questions

  1. How do I limit EE´s related categories loop so that it fills up the remaining slots?
  2. How do I avoid entry dublications? EE should not add any entries choosen by the editor.
3

Use Stash - Here's how:

{!--  ============================================
STORE A LIST OF RELATED ARTICLES INTO A LIST
================================================== --}

{!-- first the manually chosen ones --}
{exp:stash:set_list name="rels" parse_tags="yes" trim="yes"}
    {exp:channel:entries channel="article"}
        {related_articles}
            {stash:the-title}{related_articles:title}{/stash:the-title}
        {/related_articles}
    {/exp:channel:entries}
{/exp:stash:set_list}

{!-- next append the automatically chosen ones to the same list. No need for more than 4 --}
{exp:channel:entries channel="article" related_categories_mode="yes" custom_fields="yes" limit="4"}
    {exp:stash:append_list name="rels" parse_tags="yes" trim="yes"}
        {stash:the-title}{title}{/stash:the-title}
    {/exp:stash:append_list}
{/exp:channel:entries}

{!--  ============================================
OUTPUT THE DATA
================================================== --}

{exp:stash:parse process="end"}
    <ul>
    {exp:stash:get_list name="rels" unique="yes" limit="4"}
        <li>{the-title}</li>
    {/exp:stash:get_list}
    </ul>
{/exp:stash:parse}

Unique="yes" will prevent any duplicates, and setting the chosen related articles before the automatically chosen ones will ensure that the former takes precedence over the latter.

If you're using a separate embedded template for your display output you won't need the stash:parse tag, but if you're getting and setting in the same template you'll need it here to avoid parse order headaches.

5
  • Thanks, for your help. I implemented you code. The first part works, Stash retrieves the manually chosen ones, but not the automatically chosen. I added status="open|featured" to the channel entries tag, but regardless if with or without the status parameter, no entries are displayed. At the moment I have 3 channel entries tags in my article template. The stash´ed tag. I use the original relationship-tag and the original related categories tag to make sure, that there are entries to display. How can I debug? – awa Aug 20 '13 at 15:35
  • Are you definitely on a 'single entry' page? - In other words, do you have the url_title of a single entry in segment_2 or 3 of the URL? Maybe try limiting the first channel entries tag to 1 just to make sure it's not pulling multiple entries. You can remove the limit="4" on the get_list to see exactly what your variable contains. What if you remove the set_list section completely, does it output the auto relations? – James Smith Aug 20 '13 at 15:51
  • My URL is: mysite.de/templategroup/URL_title_of_SE I turned on debugging and I see 3 "SET rels to value..." Thisis good. The first is when Stash gathers my 2 manual relationships, then come 4 automatically relationships and the third is, when Stash combines them. My understanding is, that this is the list that gets parsed in the "output the data"-block. The actual list has 5 unique entiries, one is double. This is also fine, since unique="yes" will take care of that. – awa Aug 20 '13 at 16:13
  • Fixed, your code is great I only extended the parse tag to {exp:stash:parse process="end"} Found that here: devot-ee.com/add-ons/support/stash/viewthread/9242#31692 Thanks again! – awa Aug 20 '13 at 16:29
  • good stuff, have updated the answer with that parameter. Strangely it wasn't necessary on my testing environment, but there ya go... It's probably something to do with the overall nesting level of any other code in your template. – James Smith Aug 20 '13 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.