0

I'd like to use a stash'd variable to filter a list of categories, but the following is not working:

{if segment_2=="thing1"}
    {exp:stash:set name="cat_id"}22{/exp:stash:set}
{if:elseif segment_2=="thing2"}
    {exp:stash:set name="cat_id"}23{/exp:stash:set}
{/if}

{exp:stash:set_list name="filters" parse_tags="yes"}
    {exp:channel:categories channel="channel1" category_group="{exp:stash:get name='cat_id'}" backspace="6"}
        {stash:category_id}{category_id}{/stash:category_id}
        {stash:category_name}{category_name}{/stash:category_name}
        {stash:category_url_title}{category_url_title}{/stash:category_url_title}
    {/exp:channel:categories}
{/exp:stash:set_list}

I think this is a parsing issue, but how do I fix it?

Update: I tried this after the answer offered below:

{exp:switchee variable="{segment_2}" parse="inward"}    
    {case value="thing1"}
        {exp:stash:set name="cat_id"}22{/exp:stash:set}
    {/case}
    {case value="thing2"}
        {exp:stash:set name="cat_id"}23{/exp:stash:set}
    {/case}
{/exp:switchee}

{exp:stash:set_list name="filters" parse_tags="yes"}
    {exp:channel:categories channel="channel1" category_group="{exp:stash:get name='cat_id'}" backspace="6" parse="inward"}
        {stash:category_id}{category_id}{/stash:category_id}
        {stash:category_name}{category_name}{/stash:category_name}
        {stash:category_url_title}{category_url_title}{/stash:category_url_title}
    {/exp:channel:categories}
{/exp:stash:set_list}

I also tried: {exp:stash:get name='cat_id' process='end' }

But still no luck.

1 Answer 1

1

First problem: the variable "cat_id" will get set twice and always have the later value. You could use Switchee instead of if/else. The reason why and some alternative solutions are described here: https://gist.github.com/croxton/9d012297096892ca5c10

Second problem: when you pass a variable in a parameter to a tag, you need to use parse="inward" to tell EE to parse the tag.

{exp:channel:categories channel="channel1" category_group="{exp:stash:get name='cat_id'}" backspace="6" parse="inward"} 
...
{/exp:channel:categories}

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.