HUGO

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
gohugoio Star
  • About Hugo
    • Overview
    • What is Hugo
    • Hugo features
    • Static site generators
    • Hugo's security model
    • Hugo and the GDPR
    • License
  • Installation
    • Overview
    • macOS
    • Linux
    • Windows
    • BSD
  • Getting started
    • Overview
    • Quick start
    • Basic usage
    • Directory structure
    • Configuration
    • Configure markup
    • Glossary of terms
    • External learning resources
  • Quick reference
    • Overview
    • Emojis
    • Functions
    • Methods
    • Page collections
  • Content management
    • Overview
    • Organization
    • Page bundles
    • Content formats
    • Front matter
    • Build options
    • Page resources
    • Image processing
    • Shortcodes
    • Related content
    • Sections
    • Content types
    • Archetypes
    • Taxonomies
    • Summaries
    • Links and cross references
    • URL management
    • Menus
    • Static files
    • Table of contents
    • Comments
    • Multilingual
    • Markdown attributes
    • Syntax highlighting
    • Diagrams
    • Mathematics
  • Templates
    • Overview
    • Templating
    • Template lookup order
    • Base templates and blocks
    • Single page templates
    • List templates
    • Homepage template
    • Section templates
    • Taxonomy templates
    • Pagination
    • Content view templates
    • Partial templates
    • Shortcode templates
    • Menu templates
    • Data templates
    • RSS templates
    • Sitemap templates
    • Internal templates
    • Custom output formats
    • 404 page
    • Robots.txt
  • Functions
    • Overview
    • cast
    • collections
    • compare
    • crypto
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go template
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • Methods
    • Overview
    • Duration
    • Menu
    • Menu entry
    • Page
    • Pages
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
  • Render hooks
    • Overview
    • Introduction
    • Code blocks
    • Headings
    • Images
    • Links
  • Hugo Modules
    • Overview
    • Configure Hugo modules
    • Use Hugo Modules
    • Theme components
  • Hugo Pipes
    • Overview
    • Introduction
    • Transpile Sass to CSS
    • PostCSS
    • PostProcess
    • JavaScript building
    • Babel
    • Asset minification
    • Concatenating assets
    • Fingerprinting and SRI hashing
    • Resource from string
    • Resource from template
  • CLI
  • Troubleshooting
    • Overview
    • Audit
    • Logging
    • Inspection
    • Deprecation
    • Performance
    • FAQs
  • Developer tools
    • Overview
    • Editor plugins
    • Front-ends
    • Search
    • Migrations
    • Other projects
  • Hosting and deployment
    • Overview
    • Hugo Deploy
    • Deploy with Rclone
    • Deploy with Rsync
    • Host on 21YunBox
    • Host on AWS Amplify
    • Host on Azure Static Web Apps
    • Host on Cloudflare Pages
    • Host on Firebase
    • Host on GitHub Pages
    • Host on GitLab Pages
    • Host on KeyCDN
    • Host on Netlify
    • Host on Render
  • Contribute
    • Overview
    • Development
    • Documentation
    • Themes
  • Maintenance
TEMPLATES

Taxonomy templates

Taxonomy templating includes taxonomy list pages, taxonomy terms pages, and using taxonomies in your single page templates.

Hugo includes support for user-defined groupings of content called taxonomies. Taxonomies are classifications that demonstrate logical relationships between content. See Taxonomies under Content Management if you are unfamiliar with how Hugo leverages this powerful feature.

Hugo provides multiple ways to use taxonomies throughout your project templates:

  • Order the way content associated with a taxonomy term is displayed in a taxonomy list template
  • Order the way the terms for a taxonomy are displayed in a taxonomy terms template
  • List a single content’s taxonomy terms within a single page template

Taxonomy list templates

Taxonomy list page templates are lists and therefore have all the variables and methods available to list pages.

Taxonomy list template lookup order

See Template Lookup.

Taxonomy terms templates

Taxonomy terms templates lookup order

See Template Lookup.

Taxonomy methods

A Taxonomy is a map[string]WeightedPages.

.Get TERM
Returns the WeightedPages for a given term. For example: ; site.Taxonomies.tags.Get "tag-a".
.Count TERM
The number of pieces of content assigned to the given term. For example:
site.Taxonomies.tags.Count "tag-a".
.Alphabetical
Returns an OrderedTaxonomy (slice) ordered by term.
.ByCount
Returns an OrderedTaxonomy (slice) ordered by number of entries.
.Reverse
Returns an OrderedTaxonomy (slice) in reverse order. Must be used with an OrderedTaxonomy.

OrderedTaxonomy

Since Maps are unordered, an OrderedTaxonomy is a special structure that has a defined order.

[]struct {
    Name          string
    WeightedPages WeightedPages
}

Each element of the slice has:

.Term
The Term used.
.WeightedPages
A slice of Weighted Pages.
.Count
The number of pieces of content assigned to this term.
.Page
Returns a page reference for this term.
.Pages
All Pages assigned to this term. All list methods are available to this.

WeightedPages

WeightedPages is simply a slice of WeightedPage.

type WeightedPages []WeightedPage
.Count
The number of pieces of content assigned to this term.
.Page
Returns a page reference for this term.
.Pages
Returns a slice of pages, which then can be ordered using any of the list methods.

Displaying custom metadata in taxonomy terms templates

If you need to display custom metadata for each taxonomy term, you will need to create a page for that term at /content/<TAXONOMY>/<TERM>/_index.md and add your metadata in its front matter, as explained in the taxonomies documentation. Based on the Actors taxonomy example shown there, within your taxonomy terms template, you may access your custom fields by iterating through the variable .Pages as such:

<ul>
  {{ range .Pages }}
    <li>
      <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
      {{ .Params.wikipedia }}
    </li>
  {{ end }}
</ul>

Order taxonomies

Taxonomies can be ordered by either alphabetical key or by the number of content pieces assigned to that key.

Order alphabetically example

<ul>
  {{ range .Data.Terms.Alphabetical }}
    <li><a href="{{ .Page.Permalink }}">{{ .Page.Title }}</a> {{ .Count }}</li>
  {{ end }}
</ul>

Order content within taxonomies

Hugo uses both date and weight to order content within taxonomies.

Each piece of content in Hugo can optionally be assigned a date. It can also be assigned a weight for each taxonomy it is assigned to.

When iterating over content within taxonomies, the default sort is the same as that used for section and list pages: first by weight, then by date. This means that if the weights for two pieces of content are the same, then the more recent content will be displayed first.

The default weight for any piece of content is 0. Zero means “does not have a weight”, not “has a weight of numerical value zero”.

Weights of zero are thus treated specially: if two pages have unequal weights, and one of them is zero, then the zero-weighted page will always appear after the other one, regardless of the other’s weight. Zero weights should thus be used with care: for example, if both positive and negative weights are used to extend a sequence in both directions, a zero-weighted page will appear not in the middle of the list, but at the end.

Assign weight

Content can be assigned weight for each taxonomy that it’s assigned to.

content/example.md
     
---
categories:
- d
categories_weight: 44
tags:
- a
- b
- c
tags_weight: 22
title: Example
---
+++
categories = ['d']
categories_weight = 44
tags = ['a', 'b', 'c']
tags_weight = 22
title = 'Example'
+++
{
   "categories": [
      "d"
   ],
   "categories_weight": 44,
   "tags": [
      "a",
      "b",
      "c"
   ],
   "tags_weight": 22,
   "title": "Example"
}

The convention is taxonomyname_weight.

In the above example, this piece of content has a weight of 22 which applies to the sorting when rendering the pages assigned to the “a”, “b” and “c” values of the ’tag’ taxonomy.

It has also been assigned the weight of 44 when rendering the ’d’ category.

With this the same piece of content can appear in different positions in different taxonomies.

Currently taxonomies only support the default ordering of content which is weight -> date.

There are two different templates that the use of taxonomies will require you to provide.

Both templates are covered in detail in the templates section.

A list template is any template that will be used to render multiple pieces of content in a single html page. This template will be used to generate all the automatically created taxonomy pages.

A taxonomy template is a template used to generate the list of terms for a given template.

There are four common ways you can display the data in your taxonomies in addition to the automatic taxonomy pages created by hugo using the list templates:

  1. For a given piece of content, you can list the terms attached
  2. For a given piece of content, you can list other content with the same term
  3. You can list all terms for a taxonomy
  4. You can list all taxonomies (with their terms)

List terms assigned to a page

List the terms assigned to a page using the .Page.GetTerms method.

To render an unordered list:

{{ $taxonomy := "tags" }}
{{ with .GetTerms $taxonomy }}
  <p>{{ (site.GetPage $taxonomy).LinkTitle }}:</p>
  <ul>
    {{ range . }}
      <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
    {{ end }}
  </ul>
{{ end }}

To render a comma-delimited list:

{{ $taxonomy := "tags" }}
{{ with .GetTerms $taxonomy }}
  <p>
    {{ (site.GetPage $taxonomy).LinkTitle }}:
    {{ range $k, $_ := . -}}
      {{ if $k }}, {{ end }}
      <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
    {{- end }}
  </p>
{{ end }}

List content with the same taxonomy term

If you are using a taxonomy for something like a series of posts, you can list individual pages associated with the same taxonomy. This is also a quick and dirty method for showing related content:

Example: showing content in same series

<ul>
  {{ range .Site.Taxonomies.series.golang }}
    <li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a></li>
  {{ end }}
</ul>

List all content in a given taxonomy

This would be very useful in a sidebar as “featured content”. You could even have different sections of “featured content” by assigning different terms to the content.

Example: grouping “featured” content

<section id="menu">
  <ul>
    {{ range $key, $taxonomy := .Site.Taxonomies.featured }}
      <li>{{ $key }}</li>
      <ul>
        {{ range $taxonomy.Pages }}
          <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
        {{ end }}
      </ul>
    {{ end }}
  </ul>
</section>

Render a site’s taxonomies

If you wish to display the list of all keys for your site’s taxonomy, you can retrieve them from the .Site variable available on every page.

This may take the form of a tag cloud, a menu, or simply a list.

The following example displays all terms in a site’s tags taxonomy:

Example: list all site tags

<ul>
  {{ range .Site.Taxonomies.tags }}
    <li><a href="{{ .Page.Permalink }}">{{ .Page.Title }}</a> {{ .Count }}</li>
  {{ end }}
</ul>

Example: list all taxonomies, terms, and assigned content

This example will list all taxonomies and their terms, as well as all the content assigned to each of the terms.

layouts/partials/all-taxonomies.html
<ul>
  {{ range $taxonomy, $terms := site.Taxonomies }}
    <li>
      {{ with site.GetPage $taxonomy }}
        <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
      {{ end }}
      <ul>
        {{ range $term, $weightedPages := $terms }}
          <li>
            <a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a>
            <ul>
              {{ range $weightedPages }}
                <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
              {{ end }}
            </ul>
          </li>
        {{ end }}
      </ul>
    </li>
  {{ end }}
</ul>

.Site.GetPage for taxonomies

Because taxonomies are lists, the .GetPage function can be used to get all the pages associated with a particular taxonomy term using a terse syntax. The following ranges over the full list of tags on your site and links to each of the individual taxonomy pages for each term without having to use the more fragile URL construction of the “List All Site Tags” example above:

links-to-all-tags.html
{{ $taxo := "tags" }}
<ul class="{{ $taxo }}">
  {{ with ($.Site.GetPage (printf "/%s" $taxo)) }}
    {{ range .Pages }}
      <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
    {{ end }}
  {{ end }}
</ul>

See also

  • Taxonomies
  • Archetypes
  • Front matter
  • Lists of content in Hugo
  • Build options

On this page

  • Taxonomy list templates
  • Taxonomy terms templates
  • WeightedPages
  • Displaying custom metadata in taxonomy terms templates
  • Order taxonomies
  • Order content within taxonomies
  • List terms assigned to a page
  • List content with the same taxonomy term
  • List all content in a given taxonomy
  • Render a site’s taxonomies
  • .Site.GetPage for taxonomies
Last updated: February 3, 2024: Replace links to variable pages with links to method pages (126c323d)
Improve this page
By the Hugo Authors
Hugo Logo
  • File an Issue
  • Get Help
  • @GoHugoIO
  • @spf13
  • @bepsays

Netlify badge

 

Hugo Sponsors

 

The Hugo logos are copyright © Steve Francia 2013–2024.

The Hugo Gopher is based on an original work by Renée French.

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
  • About Hugo
  • Installation
  • Getting started
  • Quick reference
  • Content management
  • Templates
  • Functions
  • Methods
  • Render hooks
  • Hugo Modules
  • Hugo Pipes
  • CLI
  • Troubleshooting
  • Developer tools
  • Hosting and deployment
  • Contribute
  • Maintenance