Categories and tags

By: Dave Walsh

         hugo · sections · taxonomies      hugo · cats and tags · sections      hugo zero to hero

Estimated reading time: 2 minutes

Categories and tag sections

Having the ability to section out your site into logical areas is often essential for sites that discuss more than one thing.

add the control variables top the config.toml file:

[taxonomies]
  category = "categories"
  tag = "tags"
  series = "series"

add in the content variables to the content pages front matter:

categories: "learn hugo"
tags: "cats and tags"
series: "learn hugo"

showing the categories.

Should you want to have the post linked to more than one tag or category or series then simply create an array against the variable:

categories: ["learn hugo", "a second category"]
tags: ["hugo", "cats and tags", "sections"]
series: ["learn hugo", "second series"]

Listing categories in a page

Open your layout list.html or single.html and add in:

{{ range $name, $items := .Site.Taxonomies.categories }}
<li><a href="{{ $.Site.BaseURL }}categories/{{ $name | urlize | lower }}">{{ $name }} &nbsp;<span>({{ len $items }})</span></a></li>
{{ end }}

Listing tags in a page

Add the following to your layout template:

<ul id="all-tags">
  {{ range $name, $taxonomy := .Site.Taxonomies.tags }}
    <li><a href="{{ "/tags/" | relLangURL }}{{ $name | urlize }}">{{ $name }}</a></li>
  {{ end }}
</ul>

Listing series in a page

Add the following code to your layout template:

{{ range $name, $items := .Site.Taxonomies.series }}
<li><a href="{{ $.Site.BaseURL }}series/{{ $name | urlize | lower }}">{{ $name }} &nbsp;<span>({{ len $items }})</span></a></li>
{{ end }}

Part 11 of 15 in the hugo zero to hero series.

Series Start | No theme | Images and js