Cheat Sheet: craft.categories

{# craft.categories tag #}
{% set categories = craft.categories.first() %}
{% set categories = craft.categories.last() %}
{% set categories = craft.categories.find() %}
{% set categories = craft.categories.ids() %}
{% set categories = craft.categories.total() %}

{% set category = craft.categories({
  id:             id OR 'not id' OR '1,2,3' OR [1,2,3],
  fixedOrder:     true OR false,

  group:          'handle',
  groupId:        id,

  locale:         'en_us',
  limit:          number,
  indexBy:        'id', 'title',
  offset:         id,
  order:          'title,id,groupId,slug,uri desc',
  slug:           'slug',
  uri:            'uri',

  ancestorOf:     CategoryModel OR id,
  ancestorDist:   level, {# number of levels above ancestorOf #}
  descendantOf:   CategoryModel OR id,
  descendantDist: level, {# number of levels below descendantOf #}
  nextSiblingOf:  CategoryModel OR id,
  prevSiblingOf:  CategoryModel OR id,
  level:          level,

  relatedTo:      AssetFileModel, EntryModel, UserModel, 
                  CategoryModel, TagModel,
                  elementId,
                  [ arrayOfModels, arrayOfModels, arrayOfModels ],
                  [ 1, 2, 3 ],
                  craft.assets, craft.categories, craft.entries,
                  craft.tags, craft.users

  search:         'salty dog'           containing both "salty" and "dog"
                  '"salty dog"'         containing the exact phrase "salty dog"
                  'salty OR dog'        containing either "salty" or "dog" (or both)
                  'salty -dog'          containing "salty" but not "dog"
                  'body:salty'          containing "salty" in the "body" field
                  'body:salty'          body:dog containing both "salty" and "dog" 
                                        in the "body" field
                  'body:*'              containing anything within the "body" field
                  'salty locale:en_us'  containing "salty" in the locale "en_us"
                  'salt*'               containing a word that begins with "salt"
                  '*ty'                 containing a word that ends with "ty"
                  '*alt*'               containing a word that contains "alt",

}) %}

{% for category in categories %}
  
  {# CategoryModel #}

  {# Properties #}
  {{ user.ancestors }}           {# true,false #}
  {{ user.children }}
  {{ user.cpEditUrl }}
  {{ user.dateCreated }}
  {{ user.dateUpdated }}
  {{ user.descendants }}
  {{ user.enabled }}
  {{ user.group }}
  {{ user.id }}
  {{ user.level }}
  {{ user.link }}
  {{ user.locale }}
  {{ user.next }}
  {{ user.nextSibling }}
  {{ user.parent }}
  {{ user.prev }}
  {{ user.prevSibling }}
  {{ user.siblings }}          {# active, locked, suspended, pending, archived #}
  {{ user.slug }}
  {{ user.title }}
  {{ user.uri }}
  {{ user.url }}
             
  {# Methods #}
  {{ entry.getAncestors( distance ) }}
  {{ entry.getChildren() }}
  {{ entry.getDescendants( distance ) }}
  {{ entry.getGroup() }}
  {{ entry.getLink() }}

  {{ entry.getNextSibling() }}
  {{ entry.getParent() }}
  {{ entry.getPrevSibling() }}
  {{ entry.getSiblings() }}
  {{ entry.getUrl() }}
  {{ entry.isAncestorOf( category ) }}

  {{ entry.isChildOf( category ) }}
  {{ entry.isDescendantOf( category ) }}
  {{ entry.isNextSiblingOf( category ) }}
  {{ entry.isParentOf( category ) }}
  {{ entry.isPrevSiblingOf( category ) }}
  {{ entry.isSiblingOf( category ) }}

  {% set prev = category.getPrev( params ) %}
  {% set next = category.getNext( params ) %}
  {% if prev %} <a href="/link-to-prev-category">{{ prev.url }}</a> {% endif %}
  {% if next %} <a href="/link-to-next-category">{{ next.url }}</a> {% endif %}

{% endfor %}

Level up in Craft CMS with practical examples, snippets, and patterns.
Craft The Planet emails are sent out several times a week.