2014-05-27 18:32:57 -04:00
|
|
|
---
|
2016-01-06 17:45:19 -05:00
|
|
|
lastmod: 2015-12-23
|
2014-05-29 18:42:05 -04:00
|
|
|
date: 2014-05-26
|
|
|
|
linktitle: Usage
|
2014-05-27 18:32:57 -04:00
|
|
|
menu:
|
|
|
|
main:
|
2014-05-29 18:42:05 -04:00
|
|
|
parent: taxonomy
|
|
|
|
next: /taxonomies/displaying
|
|
|
|
prev: /taxonomies/overview
|
|
|
|
title: Using Taxonomies
|
|
|
|
weight: 15
|
2014-05-27 18:32:57 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Defining taxonomies for a site
|
|
|
|
|
2014-10-07 19:52:58 -04:00
|
|
|
Taxonomies must be defined in the site configuration before they can be
|
2014-05-27 18:32:57 -04:00
|
|
|
used throughout the site. You need to provide both the plural and
|
|
|
|
singular labels for each taxonomy.
|
|
|
|
|
2015-01-29 14:34:56 -05:00
|
|
|
Here is an example configuration in TOML and YAML
|
|
|
|
that specifies three taxonomies (the default two, plus `series`).
|
2014-05-27 18:32:57 -04:00
|
|
|
|
2015-01-29 14:34:56 -05:00
|
|
|
Notice the format is <code><strong>singular key</strong> = "<em>plural value</em>"</code> for TOML,
|
|
|
|
or <code><strong>singular key</strong>: "<em>plural value</em>"</code> for YAML:
|
2014-05-27 18:32:57 -04:00
|
|
|
|
2015-01-29 14:34:56 -05:00
|
|
|
<table class="table">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>config.toml excerpt:</th><th>config.yaml excerpt:</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<tr valign="top">
|
2015-12-23 11:31:07 -05:00
|
|
|
<td><pre><code class="language-toml">[taxonomies]
|
|
|
|
tag = "tags"
|
|
|
|
category = "categories"
|
|
|
|
series = "series"
|
2015-01-29 14:34:56 -05:00
|
|
|
</code></pre></td>
|
|
|
|
<td><pre><code class="language-yaml">taxonomies:
|
|
|
|
tag: "tags"
|
|
|
|
category: "categories"
|
|
|
|
series: "series"
|
|
|
|
</code></pre></td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2014-05-27 18:32:57 -04:00
|
|
|
|
|
|
|
## Assigning taxonomy values to content
|
|
|
|
|
|
|
|
Once an taxonomy is defined at the site level, any piece of content
|
|
|
|
can be assigned to it regardless of content type or section.
|
|
|
|
|
|
|
|
Assigning content to an taxonomy is done in the front matter.
|
|
|
|
Simply create a variable with the *plural* name of the taxonomy
|
|
|
|
and assign all terms you want to apply to this content.
|
|
|
|
|
2015-12-09 11:25:50 -05:00
|
|
|
## Preserving taxonomy values
|
|
|
|
|
|
|
|
By default, taxonomy names are hyphenated, lower-cased and normalized, and then
|
|
|
|
fixed and titleized on the archive page.
|
|
|
|
|
|
|
|
However, if you want to have a taxonomy value with special characters
|
|
|
|
such as `Gérard Depardieu` instead of `Gerard Depardieu`,
|
|
|
|
you need to set the `preserveTaxonomyNames` [site configuration](/overview/configuration/) variable to `true`.
|
|
|
|
Hugo will then preserve special characters in taxonomy values
|
|
|
|
but will still titleize the values for titles and normalize them in URLs.
|
|
|
|
|
|
|
|
Note that if you use `preserveTaxonomyNames` and intend to manually construct URLs to the archive pages,
|
|
|
|
you will need to pass the taxonomy values through the `urlize` template function.
|
2014-05-27 18:32:57 -04:00
|
|
|
|
2014-10-07 19:52:58 -04:00
|
|
|
### Front Matter Example (in TOML)
|
|
|
|
|
2015-12-23 11:31:07 -05:00
|
|
|
```toml
|
|
|
|
+++
|
|
|
|
title = "Hugo: A fast and flexible static site generator"
|
|
|
|
tags = [ "Development", "Go", "fast", "Blogging" ]
|
|
|
|
categories = [ "Development" ]
|
|
|
|
series = [ "Go Web Dev" ]
|
|
|
|
slug = "hugo"
|
|
|
|
project_url = "https://github.com/spf13/hugo"
|
|
|
|
+++
|
|
|
|
```
|
2014-10-07 19:52:58 -04:00
|
|
|
|
2014-05-27 18:32:57 -04:00
|
|
|
### Front Matter Example (in JSON)
|
|
|
|
|
2015-12-23 11:31:07 -05:00
|
|
|
```json
|
|
|
|
{
|
|
|
|
"title": "Hugo: A fast and flexible static site generator",
|
|
|
|
"tags": [
|
|
|
|
"Development",
|
|
|
|
"Go",
|
|
|
|
"fast",
|
|
|
|
"Blogging"
|
|
|
|
],
|
|
|
|
"categories" : [
|
|
|
|
"Development"
|
|
|
|
],
|
|
|
|
"series" : [
|
|
|
|
"Go Web Dev"
|
|
|
|
],
|
|
|
|
"slug": "hugo",
|
|
|
|
"project_url": "https://github.com/spf13/hugo"
|
|
|
|
}
|
|
|
|
```
|