1

Beneath each article (single entry page) I would like to show related articles.

I have four slots. For each slot I need

  • the article´s title => title field
  • the permalink => url_title field
  • the first image => Matrix field, contains only one row.

Editors choose the related articles manually via a relationships-field. If they choose less than 4 articles, the remaining slots will be filled automatically with articles from the same category.

I use Stash to gather the list.

My problem: How do I get the image? Title and permalink are no problem, but in order to get to the image, I need to go one level "deeper" and loop through the Matrix field since my image is just a subfield of the Matrix field.

This is my code the relationship field is named thematisch_passend (german for related)

    {!--  ============================================ 
    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" parse_depth="2"}
   {exp:channel:entries channel="artikel"}
      {thematisch_passend status="not closed"}
        {stash:the-title}{thematisch_passend:title}{/stash:the-title}
        {stash:the-url-title-path}{thematisch_passend:url_title_path='artikel/index'}{/stash:the-url-title-path}

            {exp:stash:set_list:nested name="related_entries context={entry_id}" parse_tags="yes"}            
            {intro_bild} <!-- name of the matrix-field -->
              {stash:the-image}{ib_file}{/stash:the-image} <!-- stashes the correct image URL -->
            {/intro_bild}
          {/exp:stash:set_list:nested}     
       {/thematisch_passend} 
    {/exp:channel:entries}
{/exp:stash:set_list}

Next I append the automatically chosen ones to the same list. I cut the code here, because the rest works.

Here´s what Debug shows

Stash: SET related_entries context=19 to value the-image|=|http://www.mysite.de/myimage.jpg

This means, that the-image has the correct value. But when Stash builds the final list, this value somehow got lost.

Stash build this list (the first ist manually and the second one automatically selected):

SET rels to value 
the-title|=|Testartikel 1|&|the-url-title-path|=|http://www.mysite.de/artikel/testartikel1|

+|the-title|=|Testartikel2|&|the-url-title-path|=|http://www.mysite.de/artikel/testartikel2|&|the-image|=|http://www.mysite.de/testartikel2_image.png

The 2nd block has an image, the first not. How can I tell stash to include the-image in this list. Stash has already stored the correct URL.

2 Answers 2

0

Does this work? The nested set_list has been removed because youve said matrix only contains one row.

{exp:stash:set_list name="rels" parse_tags="yes" trim="yes" parse_depth="2"}
   {exp:channel:entries channel="artikel"}
      {thematisch_passend status="not closed"}
        {stash:the-title}{thematisch_passend:title}{/stash:the-title}
        {stash:the-url-title-path}{thematisch_passend:url_title_path='artikel/index'}{/stash:the-url-title-path}

            {intro_bild} <!-- name of the matrix-field -->
              {stash:the-image}{ib_file}{/stash:the-image} <!-- stashes the correct image URL -->
            {/intro_bild}

       {/thematisch_passend} 
    {/exp:channel:entries}
{/exp:stash:set_list}

If not, then...

You are missing closing quote in name param and context field.

 {exp:stash:set_list:nested name="related_entries context={entry_id}" parse_tags="yes"}            

And this set_list keeps getting replaced by each loop through the channel entries tag, so add a unique id to name field like this

 {exp:stash:set_list:nested name="related_entries_{entry_id}" context="{entry_id}" parse_tags="yes"}                  
2
  • I tried your firstcode block, but I found, that I need the nesting, otherwise I get the image of the single entry I´m on and not the image of the related entry as selected via the relationship field. I reworked the exp:stash:set_list line to include the unique ID. But it´s still not working. I have the 2 titles but no image. And Debug shows only one loop for the nested stash although I have 2 related articles
    – awa
    Sep 3, 2013 at 18:00
  • Try increasing parse_depth to 3. Also, when you look at the debugger, are the nested set_list built BEFORE your get_list request? If not, then you have a parse_order issue.
    – Lloyd Hill
    Sep 3, 2013 at 21:28
0

Here´s how I solved the problem:

  • I made the image a Matrix field named intro_bild
  • thematisch_passend is the name of the EE-relationsship field. This field is used by the editors to pick related articles.
  • ib_file stands for intro_bild file. This the file upload field.

This code works for me

{!-- ============================================ STORE A LIST OF RELATED ARTICLES INTO A LIST ================================================= --} {!-- first the manually chosen ones --}

 {exp:channel:entries channel="artikel"}
      {exp:stash:set_list name="rels" parse_tags="yes" parse_depth="2"}
        {thematisch_passend status="not closed"}
          {stash:the-title}{thematisch_passend:title}{/stash:the-title}
          {stash:the-url-title-path}{thematisch_passend:url_title_path='artikel/index'}{/stash:the-url-title-path}
          {stash:the-image}{thematisch_passend:intro_bild var_prefix="mx" limit="1"}{mx:ib_file}{/thematisch_passend:intro_bild}{/stash:the-image}

        {/thematisch_passend}
      {/exp:stash:set_list}    {/exp:channel:entries}  

{!-- next append the automatically chosen ones to the same list. No need for more than 4 --} {exp:channel:entries channel="artikel" related_categories_mode="yes" custom_fields="yes" limit="4" status="open|featured"}
    {exp:stash:append_list name="rels" parse_tags="yes" trim="yes"}
        {stash:the-title}{title}{/stash:the-title}
        {stash:the-url-title-path}{url_title_path='artikel/index'}{/stash:the-url-title-path} {stash:the-image}
                    {intro_bild}
                        {ib_file}
                    {/intro_bild} {/stash:the-image}
    {/exp:stash:append_list} {/exp:channel:entries}

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

{exp:stash:parse process="end"} <ul class="small-block-grid-2 large-block-grid-4"> {exp:stash:get_list name="rels" unique="yes" limit="4"}
        <li><a href="{the-url-title-path}" title="Den gesamten Artikel &quot;{the-title}&quot; lesen">{exp:imgsizer:size src="{the-image}" width="200"}<img src="{sized}" width="{width}" alt="Den gesamten Artikel &quot;{the-title}&quot; lesen" />{/exp:imgsizer:size}</a>
        <h6 class="subheader"><a href="{the-url-title-path}">{the-title}</a></h6></li> {/exp:stash:get_list}
    </ul> {/exp:stash:parse}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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