Debian Codegraph @

docs/content/en/content-management/taxonomies.md

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