0

I'm using Stash to create an A-Z page, that will simply loop through each {title} from the Pages module, using the match/against regex as follows;

{exp:channel:entries channel="page" sort="asc" orderby="title" dynamic="no"}

    {exp:stash:set_list name="pages_a" parse_tags="yes" match="#^A#" against="{title}"}
        {stash:title_a}{title}{/stash:title_a}
        {stash:url_title_a}{url_title}{/stash:url_title_a}
    {/exp:stash:set_list}

    {exp:stash:set_list name="pages_b" parse_tags="yes" match="#^B#" against="{title}"}
        {stash:title_b}{title}{/stash:title_b}
        {stash:url_title_b}{url_title}{/stash:url_title_b}
    {/exp:stash:set_list}

    {exp:stash:set_list name="pages_c" parse_tags="yes" match="#^C#" against="{title}"}
        {stash:title_c}{title}{/stash:title_c}
        {stash:url_title_c}{url_title}{/stash:url_title_c}
    {/exp:stash:set_list}

{/exp:channel:entries}


<h4>A</h4>
<ul>
    {exp:stash:get_list name="pages_a"}
        <li><a href="/{url_title_a}">&#8594; {title_a}</a></li>
    {/exp:stash:get_list}
</ul>

<h4>B</h4>
<ul>
    {exp:stash:get_list name="pages_b"}
        <li><a href="/{url_title_b}">&#8594; {title_b}</a></li>
    {/exp:stash:get_list}
</ul>

<h4>C</h4>
<ul>
    {exp:stash:get_list name="pages_c"}
        <li><a href="/{url_title_c}">&#8594; {title_c}</a></li>
    {/exp:stash:get_list}
</ul>

However, it is only returning one entry for each get_list/letter. I was wondering if anyone can spot something that I am missing?

Admittedly the code is a little more verbose than it perhaps needs to be.

Thanks.

1 Answer 1

3

What you currently do is setting a list every time a channel entries loop runs, which is why you only get the result stored during the last loop that matched your criteria.

Put the set list tags outside the channel entries tags and just set one list at that point. Stash set list will automatically detect multiple rows and capture them.

{exp:stash:set_list name="st_pages" parse_tags="yes"}
    {exp:channel:entries channel="page" sort="asc" orderby="title" dynamic="no"}

        {stash:st_title}{title}{/stash:st_title}
        {stash:st_url_title}{url_title}{/stash:st_url_title}

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

Match against can then be used to create three lists out of one when you GET the list.

{exp:stash:get_list 
   name="st_pages" 
   match="#^B#"
   against="st_title"
}
...
{/exp:stash:get_list}  

{exp:stash:get_list 
   name="st_pages" 
   match="#^A#"
   against="st_title"
}
...
{/exp:stash:get_list}  
3
  • Thanks Jerome. That helped a great deal. I just needed to make one minor amend to your solution, changing against="{st_title}" to against="st_title". Jan 28, 2013 at 15:38
  • Owwww, sorry about that. Good catch, you are right of course. Have edited my answer. Jan 28, 2013 at 15:50
  • Also, if that list is likely to stay the same for a while, you can cache it so that you don't hit the channel:entry loop every time someone visits your page. Helps on busy sites. Jan 28, 2013 at 15:52

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.