hugo/content/en/templates/taxonomy-templates.md
Bjørn Erik Pedersen 14e369b961 Squashed 'docs/' changes from 341ecabb2..988f7d5c2
988f7d5c2 Document default `enableInlineShortcodes` value
0f604a345 Fix typo in 0.66.0 release note
26fc74fe3 How to access individual EXIF data tags
d5d3bad9a Fix localhost links
fa6921213 Update index.md
5bf558f78 Release 0.66.0
74ccdaaf5 Merge branch 'temp660'
75faa478b releaser: Add release notes to /docs for release of 0.66.0
c4a4a9922 docs: Regen CLI docs
0624ac198 Add build.UseResourceCacheWhen
58a8d7cd1 Add build options documentation
d926c595e fix typo
99713d44b resources: Add basic @import support to resources.PostCSS
224b96cf7 deploy: Implement include/exclude filters for deploy
eb1a00050 Adjusting description; WordPress with capitalized P
91d8efa22 Add another tool for migration from the Wordpress
a6938a4ac Adjust showcase description
a9c0a0a69 Adjust showcase
e5af08aa6 Adding Aether as a proposed showcase item.
0013daa34 Add hugo.IsProduction function
34c419ef3 tpl: Add math.Sqrt
5bdab0ebd Update minification.md
9039332e2 Hugo 0.65.3
1400caf3a Merge branch 'temp653'
9796bb337 releaser: Add release notes to /docs for release of 0.65.3
65b26598f Fix typo
23aa57d80 Fix crashes for 404 in IsAncestor etc.
42c54bc6c 0.65.2
67fd5c1f6 Merge branch 'temp652'
d820ac017 releaser: Add release notes to /docs for release of 0.65.2
51f0888ff Release 0.65.1
91e95260c releaser: Add release notes to /docs for release of 0.65.1
1880ebf05 fix broken link on internal.md
ffaa33889 Update migrations.md
de4d64675 Another tool for migration from Medium platform
90b178d77 releaser: Add release notes to /docs for release of 0.65.1
6925cda30 Handle corner case with rendering text as code in URL
3cb4b19dd Release 0.65.0
7a600cb99 Merge branch 'temp650'
ef9531ff6 releaser: Add release notes to /docs for release of 0.65.0
9bc19606f docs: Regenerate CLI docs
d4a886ed2 Add Page.GetTerms
a3bf273a5 fix broken link on use-modules.md
001f52f4e Fix mage URL in development.md
eef72e887 Merge commit '4b670bc8cc38103c2c60e5090c2f56bf30832b8d'
b18a76631 commands: Support "hugo mod get -u ./..."

git-subtree-dir: docs
git-subtree-split: 988f7d5c2d7a1d40ec2c8ab961cb5a4e41b5bd4c
2020-03-09 20:19:32 +01:00

13 KiB

title description date publishdate lastmod categories keywords menu weight sections_weight draft aliases toc
Taxonomy Templates Taxonomy templating includes taxonomy list pages, taxonomy terms pages, and using taxonomies in your single page templates. 2017-02-01 2017-02-01 2017-02-01
templates
taxonomies
metadata
front matter
terms
templates
docs
parent weight
templates 50
50 50 false
/taxonomies/displaying/
/templates/terms/
/indexes/displaying/
/taxonomies/templates/
/indexes/ordering/
/templates/taxonomies/
/templates/taxonomy/
true

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:

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 Template

Taxonomy Terms Templates Lookup Order

See Template Lookup.

Taxonomy Methods

A Taxonomy is a map[string]WeightedPages.

.Get(term)
Returns the WeightedPages for a term.
.Count(term)
The number of pieces of content assigned to this term.
.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.
.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(term)
The number of pieces of content assigned to 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="{{ .Permalink }}">{{ .Title }}</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

In Hugo 0.55 and later you can do:

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

Before that you would have to do something like:

<ul>
    {{ $type := .Type }}
    {{ range $key, $value := .Data.Terms.Alphabetical }}
        {{ $name := .Name }}
        {{ $count := .Count }}
        {{ with $.Site.GetPage (printf "/%s/%s" $type $name) }}
            <li><a href="{{ .Permalink }}">{{ $name }}</a> {{ $count }}</li>
        {{ end }}
    {{ 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, than the more recent content will be displayed first. The default weight for any piece of content is 0.

Assign Weight

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

+++
tags = [ "a", "b", "c" ]
tags_weight = 22
categories = ["d"]
title = "foo"
categories_weight = 44
+++
Front Matter with weighted tags and categories

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 terms 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)

Display a Single Piece of Content's Taxonomies

Within your content templates, you may wish to display the taxonomies that piece of content is assigned to.

Because we are leveraging the front matter system to define taxonomies for content, the taxonomies assigned to each content piece are located in the usual place (i.e., .Params.<TAXONOMYPLURAL>).

Example: List Tags in a Single Page Template

{{< new-in "0.65.0" >}}

<ul>
    {{ range (.GetTerms "tags") }}
        <li><a href="{{ .Permalink }}">{{ .LinkTitle }}</a></li>
   {{ end }}
</ul>

Before Hugo 0.65.0 you needed to do something like this:

{{ $taxo := "tags" }} <!-- Use the plural form here -->
<ul id="{{ $taxo }}">
    {{ range .Param $taxo }}
        {{ $name := . }}
        {{ with $.Site.GetPage (printf "/%s/%s" $taxo ($name | urlize)) }}
            <li><a href="{{ .Permalink }}">{{ $name }}</a></li>
        {{ end }}
    {{ end }}
</ul>

If you want to list taxonomies inline, you will have to take care of optional plural endings in the title (if multiple taxonomies), as well as commas. Let's say we have a taxonomy "directors" such as directors: [ "Joel Coen", "Ethan Coen" ] in the TOML-format front matter.

To list such taxonomies, use the following:

Example: Comma-delimit Tags in a Single Page Template

{{ $taxo := "directors" }} <!-- Use the plural form here -->
{{ with .Param $taxo }}
    <strong>Director{{ if gt (len .) 1 }}s{{ end }}:</strong>
    {{ range $index, $director := . }}
        {{- if gt $index 0 }}, {{ end -}}
        {{ with $.Site.GetPage (printf "/%s/%s" $taxo $director) -}}
            <a href="{{ .Permalink }}">{{ $director }}</a>
        {{- end -}}
    {{- end -}}
{{ end }}

Alternatively, you may use the delimit template function as a shortcut if the taxonomies should just be listed with a separator. See {{< gh 2143 >}} on GitHub for discussion.

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.

<section id="menu">
    <ul>
        {{ range $key, $taxonomy := .Site.Taxonomies.featured }}
        <li>{{ $key }}</li>
        <ul>
            {{ range $taxonomy.Pages }}
            <li hugo-nav="{{ .RelPermalink}}"><a href="{{ .Permalink}}">{{ .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

In Hugo 0.55 and later you can simply do:

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

Before that you would do something like this:

{{< todo >}}Clean up rest of the taxonomy examples re Hugo 0.55.{{< /todo >}}

<ul id="all-tags">
    {{ range $name, $taxonomy := .Site.Taxonomies.tags }}
        {{ with $.Site.GetPage (printf "/tags/%s" $name) }}
            <li><a href="{{ .Permalink }}">{{ $name }}</a></li>
        {{ end }}
    {{ 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.

{{< code file="layouts/partials/all-taxonomies.html" download="all-taxonomies.html" download="all-taxonomies.html" >}}

    {{ range $taxonomy_term, $taxonomy := .Site.Taxonomies }} {{ with $.Site.GetPage (printf "/%s" $taxonomy_term) }}
  • {{ $taxonomy_term }}
      {{ range $key, $value := $taxonomy }}
    • {{ $key }}
      • {{ range $value.Pages }}
      • {{ .LinkTitle }}
      • {{ end }}
      {{ end }}
  • {{ end }} {{ end }}
{{< /code >}}

.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]({{< relref "#example-list-all-site-tags" >}}):

{{< code file="links-to-all-tags.html" >}} {{ $taxo := "tags" }}

    {{ with ($.Site.GetPage (printf "/%s" $taxo)) }} {{ range .Pages }}
  • {{ .Title}}
  • {{ end }} {{ end }}
{{< /code >}}