3

I have a site where there is an uneven depth of categories for a product channel. Previously I've cobbled together a band-aid solution but I'm reaching out to see if there is a better way.

I have a simple category:

RootA
 -- AChild01
 -- AChild02
RootB
RootC

The URL would be:

/products/RootA/ -- Show Child categories
/products/RootA/AChild01 -- Show entries
/products/RootB -- Show entries
/products/ -- Show RootA, RootB & RootC

Basically I need to know if segment_2 has child categories and if so then display Child category links. If segment_2 doesnt have child categories then display entries assigned to it.

Not keen on using a 'category' keyword in the URL, familiar with Switchee and GWcode categories and am using stash (if that helps) and all takes place inside a single template as categories will change over time (names not depths).

How have others dealt with a clean URL structures and uneven category depths ?

1 Answer 1

2

So with some trickery here's how I managed to get it working, you'll need: Stash, GWcode Categories & Switchee

URLS conform to either of these: mysite.com/products/category/list-entires mysite.com/products/category/sub-category/list-entries

{!-- Create list of top level categories WITHOUT children --}
{exp:stash:set name="flag_categories_1" save="yes" replace="no" parse_tags="yes"}{exp:gwcode_categories channel="flags" style="linear" show_empty="yes" depth="1" last_only="yes" backspace="1"}{cat_url_title}|{/exp:gwcode_categories}{/exp:stash:set}
{!-- Create list of top level categories WITH CHILDREN --}
{exp:stash:set name="flag_categories_2" save="yes" replace="no" parse_tags="yes"}{exp:gwcode_categories channel="flags" style="linear" show_empty="yes" depth="2" backspace="1"}{cat_url_title}|{/exp:gwcode_categories}{/exp:stash:set}
{!-- Crete list of Sub categories --}
{exp:stash:set name="has_child" save="yes" replace="no" parse_tags="yes" parse_condtionals="yes" trim="yes"}{exp:gwcode_categories channel="flags" depth="2" style="linear" backspace="1" show_empty="yes"}{parent_url_title}|{/exp:gwcode_categories}{/exp:stash:set}

All that above does is record stashes for Categories with No children, Categories with Children and a list of child categories.

Then we test segments, starting with the outermost (segment_3).

{exp:switchee variable="{segment_3}" parse="inward"}
{!-- Seg 3 is a child category - Show list of entries--}
        {case value="{stash:flag_categories_2}"}
        …
        {/case}
{!--Seg3 not valid, Seg2 - not valid. Show root categories--}
    {case default="yes"}
        {switchee variable="{segment_2}" parse="inward"}
        {case value=""}
        …
        {/case} 
{!-- Seg2 Category Has children, show child categories--}
        {case value="{stash:has_child}"}
        …
        {/case}
{!-- Seg2 - valid category with NO children, show list of entries --}
        {case value="{stash:flag_categories_2}" default="yes"}
        …
        {/case}
        {/switchee}
    {/case}
{/exp:switchee}

Hopefully this may help others trying to figure out something similar. Or chime in with your preferred method.

3
  • Thanks for posting this. This really helped me with something that had me stumped. Brilliant.
    – JKM
    Jan 14, 2014 at 3:46
  • Do you mind me asking what you did for the single product view? I have a similar situation. I have cats and sub cats. I'm using switchee to display different views, all cats, single cats, sub cats and then the single products and having a heck of a time. Sep 18, 2016 at 15:29
  • All depends on what your situation is. This answer is more to do with uneven category listings. An entries categories are simpler to configure. Open a new question referencing this post and I'll have a lool Sep 19, 2016 at 0:55

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.