1

I have a channel (products) where each entry has only one category. Each entry also has a custom field (year) which stores a year value (e.g. 2015).

The output I currently have looks like this (note the non-grouped categories):

  • 2015
    • Category 1
      • Product A
      • Product B
    • Category 2
      • Product C
    • Category 3
      • Product D
    • Category 2
      • Product E
  • 2014
    • Category 2
      • Product F etc

I'm trying to output the entries so they're grouped first by year then by the category. E.g:

  • 2015
    • Category 1
      • Product A
      • Product B
    • Category 2
      • Product C
      • Product E
  • 2014
    • Category 2
      • Product F

etc

So far I've managed to achieve most of this using Stash and IfElse to get the output structured by year then the entries themselves but I'm struggling on how to get the entries grouped by category.

My code looks like:

{exp:channel:entries channel="products" dynamic="no" disable="pagination|member_data" status="open|featured" orderby="launched"}
    {exp:stash:append name="products_{launched}" parse="inward"}
    <div>
        <h3>{categories}{category_name}{/categories}</h3>
        <a href="">{title}</a>
    </div>
    {/exp:stash:append} 
{/exp:channel:entries}

{exp:stash:set_list name="the_products" parse_tags="yes"}
  {exp:channel:entries channel="products" status="open|featured" orderby="launched"}
    {stash:item_title}{title}{/stash:item_title}
    {stash:item_launched}{launched}{/stash:item_launched}
    {stash:item_category}{categories}{category_name}{/category}{/stash:item_category}
  {/exp:channel:entries}
{/exp:stash:set_list}

{exp:stash:set_value name="prev_year" value="0" parse="inward"}

{exp:stash:get_list name="the_products" unique="item_launched" match="#.*\S.*#" against="item_launched"}
    {exp:stash:set_value name="launched_year" value="{launched}" parse="inward"}

    {exp:ifelse parse="outward"}
        {if {exp:stash:not_empty name="launched_year"}} 
            {exp:stash:set_value name="launched_year" value="{item_launched}"}
            {if "{exp:stash:launched_year}" == "{exp:stash:prev_year}"}

                {exp:stash:set_value name="prev_year" value="{item_launched}"}

            {if:else}

                <h2>{exp:stash:launched_year}</h2>

                {exp:stash:get name="products_{item_launched}"}

            {/if}
        {/if}
    {/exp:ifelse}    
{/exp:stash:get_list}

Am I on the right track with this please?

0

Your Answer

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

Browse other questions tagged or ask your own question.