| 1 |
--- |
| 2 |
title: Taxonomies |
| 3 |
description: Hugo includes support for user-defined taxonomies. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
aliases: [/taxonomies/overview/,/taxonomies/usage/,/indexes/overview/,/doc/indexes/,/extras/indexes] |
| 7 |
--- |
| 8 |
|
| 9 |
## What is a taxonomy? |
| 10 |
|
| 11 |
Hugo includes support for user-defined groupings of content called **taxonomies**. Taxonomies are classifications of logical relationships between content. |
| 12 |
|
| 13 |
Taxonomy |
| 14 |
: A categorization that can be used to classify content |
| 15 |
|
| 16 |
Term |
| 17 |
: A key within the taxonomy |
| 18 |
|
| 19 |
Value |
| 20 |
: A piece of content assigned to a term |
| 21 |
|
| 22 |
## Example taxonomy |
| 23 |
|
| 24 |
Let's assume you are making a website about movies. You may want to include the following taxonomies: |
| 25 |
|
| 26 |
- Actors |
| 27 |
- Directors |
| 28 |
- Studios |
| 29 |
- Genre |
| 30 |
- Year |
| 31 |
- Awards |
| 32 |
|
| 33 |
Then, in each of the movies, you would specify terms for each of these taxonomies (i.e., in the front matter of each of your movie content files). From these terms, Hugo would automatically create pages for each Actor, Director, Studio, Genre, Year, and Award, with each listing all of the Movies that matched that specific Actor, Director, Studio, Genre, Year, and Award. |
| 34 |
|
| 35 |
### Movie taxonomy organization |
| 36 |
|
| 37 |
To continue with the example of a movie site, the following demonstrates content relationships from the perspective of the taxonomy: |
| 38 |
|
| 39 |
```txt |
| 40 |
Actor <- Taxonomy |
| 41 |
Bruce Willis <- Term |
| 42 |
The Sixth Sense <- Value |
| 43 |
Unbreakable <- Value |
| 44 |
Moonrise Kingdom <- Value |
| 45 |
Samuel L. Jackson <- Term |
| 46 |
Unbreakable <- Value |
| 47 |
The Avengers <- Value |
| 48 |
xXx <- Value |
| 49 |
``` |
| 50 |
|
| 51 |
From the perspective of the content, the relationships would appear differently, although the data and labels used are the same: |
| 52 |
|
| 53 |
```txt |
| 54 |
Unbreakable <- Value |
| 55 |
Actors <- Taxonomy |
| 56 |
Bruce Willis <- Term |
| 57 |
Samuel L. Jackson <- Term |
| 58 |
Director <- Taxonomy |
| 59 |
M. Night Shyamalan <- Term |
| 60 |
... |
| 61 |
Moonrise Kingdom <- Value |
| 62 |
Actors <- Taxonomy |
| 63 |
Bruce Willis <- Term |
| 64 |
Bill Murray <- Term |
| 65 |
Director <- Taxonomy |
| 66 |
Wes Anderson <- Term |
| 67 |
... |
| 68 |
``` |
| 69 |
|
| 70 |
### Assign terms in front matter |
| 71 |
|
| 72 |
Continuing with the example above, assign the terms for each movie in its front matter. Use the plural name of each taxonomy as the field name, and assign the terms as an array, even when a movie has only one term for a given taxonomy: |
| 73 |
|
| 74 |
{{< code-toggle file=content/movies/unbreakable.md fm=true >}} |
| 75 |
title = 'Unbreakable' |
| 76 |
actors = ['Bruce Willis','Samuel L. Jackson'] |
| 77 |
directors = ['M. Night Shyamalan'] |
| 78 |
{{< /code-toggle >}} |
| 79 |
|
| 80 |
Each term is a string, and a taxonomy is a flat list of terms rather than a nested data structure. To associate additional data with a term, create a page for the term as described in [Metadata](#metadata). |
| 81 |
|
| 82 |
### Default destinations |
| 83 |
|
| 84 |
When taxonomies are used Hugo will automatically create both a page listing all the taxonomy's terms and individual pages with lists of content associated with each term. For example, a `categories` taxonomy declared in your configuration and used in your content front matter will create the following pages: |
| 85 |
|
| 86 |
- A single page at `example.com/categories/` that lists all the terms within the taxonomy |
| 87 |
- Individual taxonomy list pages (e.g., `/categories/development/`) for each of the terms that shows a listing of all pages marked as part of that taxonomy within any content file's front matter |
| 88 |
|
| 89 |
## Configuration |
| 90 |
|
| 91 |
See [configure taxonomies][]. |
| 92 |
|
| 93 |
## Assign terms to content |
| 94 |
|
| 95 |
To assign one or more terms to a page, create a front matter field using the plural name of the taxonomy, then add terms to the corresponding array. For example: |
| 96 |
|
| 97 |
{{< code-toggle file=content/example.md fm=true >}} |
| 98 |
title = 'Example' |
| 99 |
tags = ['Tag A','Tag B'] |
| 100 |
categories = ['Category A','Category B'] |
| 101 |
{{< /code-toggle >}} |
| 102 |
|
| 103 |
## Taxonomic weight |
| 104 |
|
| 105 |
{{% glossary-term "taxonomic weight" %}} |
| 106 |
|
| 107 |
Assign a taxonomic weight using a front matter key named `[taxonomy_name]_weight`. |
| 108 |
|
| 109 |
{{< code-toggle file="content/courses/organic-chemistry.md" fm=true >}} |
| 110 |
title = 'Organic Chemistry' |
| 111 |
weight = 10 |
| 112 |
tags_weight = 1000 |
| 113 |
tags = ['chemistry','science'] |
| 114 |
{{</ code-toggle >}} |
| 115 |
|
| 116 |
With the front matter above, the `organic-chemistry` page will float towards the top of the list on section and home pages, and it will sink towards the bottom of the list on the `chemistry` and `science` term pages. |
| 117 |
|
| 118 |
## Metadata |
| 119 |
|
| 120 |
Display metadata about each term by creating a corresponding branch bundle in the `content` directory. |
| 121 |
|
| 122 |
For example, create an `authors` taxonomy: |
| 123 |
|
| 124 |
{{< code-toggle file=hugo >}} |
| 125 |
[taxonomies] |
| 126 |
author = 'authors' |
| 127 |
{{< /code-toggle >}} |
| 128 |
|
| 129 |
Then create content with one [branch bundle](g) for each term: |
| 130 |
|
| 131 |
```tree |
| 132 |
content/ |
| 133 |
└── authors/ |
| 134 |
├── jsmith/ |
| 135 |
│ ├── _index.md |
| 136 |
│ └── portrait.jpg |
| 137 |
└── rjones/ |
| 138 |
├── _index.md |
| 139 |
└── portrait.jpg |
| 140 |
``` |
| 141 |
|
| 142 |
Then add front matter to each term page: |
| 143 |
|
| 144 |
{{< code-toggle file=content/authors/jsmith/_index.md fm=true >}} |
| 145 |
title = 'John Smith' |
| 146 |
affiliation = 'University of Chicago' |
| 147 |
{{< /code-toggle >}} |
| 148 |
|
| 149 |
Then create a _taxonomy_ template specific to the `authors` taxonomy: |
| 150 |
|
| 151 |
```go-html-template {file="layouts/authors/taxonomy.html"} |
| 152 |
{{ define "main" }} |
| 153 |
<h1>{{ .Title }}</h1> |
| 154 |
{{ .Content }} |
| 155 |
{{ range .Data.Terms.Alphabetical }} |
| 156 |
<h2><a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a></h2> |
| 157 |
<p>Affiliation: {{ .Page.Params.Affiliation }}</p> |
| 158 |
{{ with .Page.Resources.Get "portrait.jpg" }} |
| 159 |
{{ with .Fill "100x100" }} |
| 160 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="portrait"> |
| 161 |
{{ end }} |
| 162 |
{{ end }} |
| 163 |
{{ end }} |
| 164 |
{{ end }} |
| 165 |
``` |
| 166 |
|
| 167 |
In the example above we list each author including their affiliation and portrait. |
| 168 |
|
| 169 |
Or create a _term_ template specific to the `authors` taxonomy: |
| 170 |
|
| 171 |
```go-html-template {file="layouts/authors/term.html"} |
| 172 |
{{ define "main" }} |
| 173 |
<h1>{{ .Title }}</h1> |
| 174 |
<p>Affiliation: {{ .Params.affiliation }}</p> |
| 175 |
{{ with .Resources.Get "portrait.jpg" }} |
| 176 |
{{ with .Fill "100x100" }} |
| 177 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="portrait"> |
| 178 |
{{ end }} |
| 179 |
{{ end }} |
| 180 |
{{ .Content }} |
| 181 |
{{ range .Pages }} |
| 182 |
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2> |
| 183 |
{{ end }} |
| 184 |
{{ end }} |
| 185 |
``` |
| 186 |
|
| 187 |
In the example above we display the author including their affiliation and portrait, then a list of associated content. |
| 188 |
|
| 189 |
[configure taxonomies]: /configuration/taxonomies/ |