Getting a List of Category Groups in Craft CMS

How to output a list of category groups in a Twig template.

Image

Craft gives us a lot of ways to out­put cat­e­gories in a Twig tem­plate. How­ev­er, it’s not clear­ly doc­u­ment­ed how to out­put a cat­e­go­ry group. But, with the help of craft.app and the getallGroups() method avail­able in Craft, we can eas­i­ly get what we need.

Here’s an exam­ple imple­men­ta­tion using a sim­ple unordered list and out­putting only the group name:

{% set categoryGroups = craft.app.categories.getAllGroups() %}

<ul>
{% for group in categoryGroups %}
  <li>{{ group.name }}</li>
{% endfor %}
</ul>

To find out what oth­er prop­er­ties are avail­able for a group, you can use {{ dd(group) }} to dump and die a group and get a glimpse of the internals.

You’ll get some­thing like this:

CategoryGroup {#1752 ▼
  +id: 1
  +structureId: 1
  +fieldLayoutId: 6
  +name: "Blog"
  +handle: "blog"
  +maxLevels: null
  +uid: "d1ff62a0-5c47-45e9-b544-fd7e429f706d"
  -_siteSettings: null
  -_errors: null
  -_validators: null
  -_scenario: "default"
  -_events: []
  -_eventWildcards: []
  -_behaviors: array:1 [▶]
}