1

So I have a categories with sub categories.

Anakena (Wine Brand Name)
-Ona (Wine line)

--product one
--product two

-Tama (Wine Line)

--product one
--product two

Im trying to make links to the specific lines. So I want to add Anakena under a field, and have it display an ul of Ona, Tama, etc. all the subcategories within Anakena. Once I click on this link, will take me to the category page with all of Ona's produtcs, etc..

{% nav category in craft.categories.group('products').slug('anakena') %}
   <a href="{{ category.url }}">{{ category.title }}<a>
{% endnav %}

With this it gives me the main category, Anakena, how can I display the child categories assigned to Anakena.?

Thanks

  • Great,! Thanks Mats.. One more thing now, Is there a way to replace the .slug name with a user defined something.? each entry will have a different slug, can I have the name filled by an entry.? {% set category = craft.categories.group('products').slug('{{ user defines entry?? }}').first() %} Thanks – fidel Aug 7 '16 at 7:04
  • Sure, just do {% set category = craft.categories.group('products').slug(entry.someCustomField) %}. Don't echo the variable inside the .slug() parameter, i.e. don't use {{ }}. – Mats Mikkel Rummelhoff Aug 7 '16 at 10:34
1

By far, the easiest way to do this is to use the {% children %} tag which is available inside a {% nav %}{% endnav %} tag pair, i.e. something like this:

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

Note: The above will print a recursive, unordered list of all your top level product categories (i.e. "Akana", "Tama" etc) and their children.

In your current code, you won't actually be able to use the {% children %}, because you have a slug('akana') parameter on your craft.categories query for some reason. Using the slug() parameter means that Craft will only pull categories with that particular slug – in other words, the child category "Ona" won't be returned by your query and the {% children %} tag will output nothing.

If you have to use the slug parameter, there's no real use to the `{% nav %} tag (it's designed for recursive, hierarchical lists).

For a more "manual" approach, you can also get to a categories children by doing something like this:

{% set category = craft.categories.group('products').slug('akana').first() %}
<a href="{{ category.url }}">{{ category.title }}</a>
{% set children = category.children %}
{% for child in children %}
    <a href="{{ category.url }}">{{ category.title }}</a>
{% endfor %}

One gotcha to note is that category.children will pull all your category's children, from all levels. To get children for different levels, you can use the level parameter:

{% set childrenA = category.children.level(2) %}
{% set childrenB = category.children.level(3) %)    
| improve this answer | |

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.