1

I'm looking to only display the top level parent categories on my Template, but unfortunately, everything I try seems to error out and still display all of the children categories as well.

My current code looks like the following:

  {% set categories = craft.categories.group('discounts') %}
        {% for category in categories.all() %}
2

You can filter elements based on their hierarchy level with the level attribute

{% for category in categories.level(1).all() %} 

Will only show top tier categories

{% for category in categories.level(2).all() %}

only all the first tier children of all first tier categories (so tier 2) and so on.

Additional note

You can also search for descendants elements

element.getDescendants(1) //<-- include the level based on the parents level

With the parameter 1 it is the same as

element.getChildren()

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.