Debian Codegraph @

docs/content/en/methods/site/Taxonomies.md

raw
1 ---
2 title: Taxonomies
3 description: Returns a data structure containing the site's Taxonomy objects, the terms within each Taxonomy object, and the pages to which the terms are assigned.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 returnType: page.TaxonomyList
9 signatures: [SITE.Taxonomies]
10 ---
11
12 ## Usage
13
14 Conceptually, the `Taxonomies` method on a `Site` object returns a data structure such as:
15
16 <!-- markdownlint-disable MD007 MD032 -->
17 {{< code-toggle file=hugo >}}
18 taxonomy a:
19 - term 1:
20 - page 1
21 - page 2
22 - term 2:
23 - page 1
24 taxonomy b:
25 - term 1:
26 - page 2
27 - term 2:
28 - page 1
29 - page 2
30 {{< /code-toggle >}}
31 <!-- markdownlint-enable MD007 MD032 -->
32
33 For example, on a book review site you might create two taxonomies; one for genres and another for authors.
34
35 With this project configuration:
36
37 {{< code-toggle file=hugo >}}
38 [taxonomies]
39 genre = 'genres'
40 author = 'authors'
41 {{< /code-toggle >}}
42
43 And this content structure:
44
45 ```tree
46 content/
47 ├── books/
48 │ ├── and-then-there-were-none.md --> genres: suspense
49 │ ├── death-on-the-nile.md --> genres: suspense
50 │ └── jamaica-inn.md --> genres: suspense, romance
51 │ └── pride-and-prejudice.md --> genres: romance
52 └── _index.md
53 ```
54
55 Conceptually, the taxonomies data structure looks like:
56
57 <!-- markdownlint-disable MD007 MD032 -->
58 {{< code-toggle file=hugo >}}
59 genres:
60 - suspense:
61 - And Then There Were None
62 - Death on the Nile
63 - Jamaica Inn
64 - romance:
65 - Jamaica Inn
66 - Pride and Prejudice
67 authors:
68 - achristie:
69 - And Then There Were None
70 - Death on the Nile
71 - ddmaurier:
72 - Jamaica Inn
73 - jausten:
74 - Pride and Prejudice
75 {{< /code-toggle >}}
76 <!-- markdownlint-enable MD007 MD032 -->
77
78 To list the "suspense" books:
79
80 ```go-html-template
81 <ul>
82 {{ range .Site.Taxonomies.genres.suspense }}
83 <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
84 {{ end }}
85 </ul>
86 ```
87
88 Hugo renders this to:
89
90 ```html
91 <ul>
92 <li><a href="/books/and-then-there-were-none/">And Then There Were None</a></li>
93 <li><a href="/books/death-on-the-nile/">Death on the Nile</a></li>
94 <li><a href="/books/jamaica-inn/">Jamaica Inn</a></li>
95 </ul>
96 ```
97
98 > [!NOTE]
99 > Hugo's taxonomy system is powerful, allowing you to classify content and create relationships between pages.
100 >
101 > Please see the [taxonomies][] section for a complete explanation and examples.
102
103 ## Examples
104
105 The following examples demonstrate common uses of a site's taxonomies.
106
107 ### Content by taxonomy term
108
109 If you are using a taxonomy for something like a series of posts, you can list individual pages associated with the same term. For example:
110
111 ```go-html-template
112 <ul>
113 {{ range .Site.Taxonomies.series.golang }}
114 <li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a></li>
115 {{ end }}
116 </ul>
117 ```
118
119 ### All content in a taxonomy
120
121 This is useful in a sidebar as "featured content". You could even have different sections of "featured content" by assigning different terms to the content.
122
123 ```go-html-template
124 <section id="menu">
125 <ul>
126 {{ range $term, $taxonomy := .Site.Taxonomies.featured }}
127 <li>{{ $term }}</li>
128 <ul>
129 {{ range $taxonomy.Pages }}
130 <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
131 {{ end }}
132 </ul>
133 {{ end }}
134 </ul>
135 </section>
136 ```
137
138 ### Render a site's taxonomies
139
140 The following example displays all terms in a site's tags taxonomy:
141
142 ```go-html-template
143 <ul>
144 {{ range .Site.Taxonomies.tags }}
145 <li><a href="{{ .Page.Permalink }}">{{ .Page.Title }}</a> {{ .Count }}</li>
146 {{ end }}
147 </ul>
148 ```
149
150 This example will list all taxonomies and their terms, as well as all the content assigned to each of the terms.
151
152 ```go-html-template {file="layouts/_partials/all-taxonomies.html"}
153 {{ with .Site.Taxonomies }}
154 {{ $numberOfTerms := 0 }}
155 {{ range $taxonomy, $terms := . }}
156 {{ $numberOfTerms = len . | add $numberOfTerms }}
157 {{ end }}
158
159 {{ if gt $numberOfTerms 0 }}
160 <ul>
161 {{ range $taxonomy, $terms := . }}
162 {{ with $terms }}
163 <li>
164 <a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a>
165 <ul>
166 {{ range $term, $weightedPages := . }}
167 <li>
168 <a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a>
169 <ul>
170 {{ range $weightedPages }}
171 <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
172 {{ end }}
173 </ul>
174 </li>
175 {{ end }}
176 </ul>
177 </li>
178 {{ end }}
179 {{ end }}
180 </ul>
181 {{ end }}
182 {{ end }}
183 ```
184
185 [taxonomies]: /content-management/taxonomies/