2

I'm looking to use multiple category group as a navigation. The categories have 3 levels and will be listed depending on set variable. I can list all categories in the group with the following:

{% set categories = craft.categories.group('categories') %}
<ul>
    {% nav category in categories %}
        <li>
            <a href="{{ category.slug }}">{{ category.title }}</a>
            {% ifchildren %}
                <ul>{% children %}</ul>
            {% endifchildren %}     
        </li>
    {% endnav %}
</ul>

However I would like to list only the third level categories after the top level and secondary level have been set. I've tried playing around with descendantOf but to no avail. Any help to point me in the right direction much appreciated.

---Final code---- After @damon input below this is what I ended up with more or less the same:

{# ---- Set Default Segment ---- #}
{% set segment = 'default' %}
{# ---- If url segment is set, set segment to it ---- #}
{% if craft.request.getSegment(2) %}
    {% set segment = craft.request.getSegment(2) %}
{% endif %}
{% set cat = craft.categories.group('categories').slug(segment).first() %}
{% for categories in craft.categories.group('categories').descendantOf(cat).level(2) %}
    {% if categories.getDescendants().total() != 0 %}
        {% for category in craft.categories.group('categories').descendantOf(categories.id).level(3) %}
            ...
        {% endfor %}
    {% endif %}
{% endfor %}
2

I don't think I quite follow you, so forgive me if I'm way off. I have created (what I think) is something similar to what you are looking for. This is how I did it - maybe it will help...

Let's say I have the following url:

http://www.mystore.com/toys/my-awesome-toy

twig

{% set cat = craft.categories.slug(craft.request.getSegment(2)).first() %} {# Toys #}

{% for toys in craft.categories.group('toys').descendantOf(cat).level(2) %}
    {% if toys.getDescendants().total() != 0 %}
        {% for subToys in craft.categories.group('toys').descendantOf(toys.id).level(3) %}
            ...
        {% endfor %}
    {% endif %}
{% endfor %}
  • cheers that's got me on the right track – Lettie Nov 18 '15 at 16:59
  • Awesome - glad it helped!. Can you update your post to show what you ended up with? – Damon Nov 18 '15 at 19:30

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.