17

I would like to output a list of all the categories in a category group on the front end. How do I do that?

27

You can fetch your categories on the front end using craft.categories, which has a group param that can be used to filter categories by a given group handle.

{% set categories = craft.categories.group('myGroupHandle') %}

If the category group is limited to a single level, you can loop through those using a basic for-loop:

<ul>
    {% for category in categories %}
        <li>{{ category.title }}</li>
    {% endfor %}
</ul>

If the category group can be multi-level, you might be better off using the {% nav %} tag instead:

<ul>
    {% nav category in categories %}
        <li>
            {{ category.title }}

            {# Output a nested <ul> if this category has any children #}
            {% ifchildren %}
                <ul>{% children %}</ul>
            {% endifchildren %}
        </li>
    {% endnav %}
</ul>
  • Set up on craft is through the settings, then category button. Not through category tab. – Matteo Aug 20 '15 at 18:00

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.