mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-14 20:37:55 -05:00
13e64d7276
6b00298bb Remove outdated "related example" 987f1e1cd Fix dead links (#601) 96287a20a Add config option "summaryLength" (#600) ced7f2085 Adjust Over showcase e334a6354 Add new showcase: over 10435b502 Add warning about privacy options only work with internal templates (#525) 48c6b0e4d Minor grammatical fix 684670ebc Add quote 0e9fada52 Improvements to taxonomy template examples e06c4bf73 Add syntax highlighting; consistent 4-space indentation c1cb3f081 Remove dead links for custom permalinks 3e3aefd04 Fix 0a671bc3751479e74a0a9d2132736c61d239707c d65888685 fix file name in 'Add Non-content Entries to a Menu' code toggle (#547) 1a0563857 Add Solus install guide (#590) 8a0d65b0d Update Windows Installation instructions (#564) c4348636a Fix typo 0a671bc37 Add post to menu example af14497c6 Add notes for `os.Stat` (Hugo 0.47) (#557) e49f65bb3 Singular to plural cb5608dbf Update introduction.md 30b060dff Add variable re-definition example (Hugo v0.48+) 21123967e Minor edits fac3df043 Refresh the Go Templates introduction 4a9600e92 Updating URL to how-to-guide for hosting hugo site on firebase bfaa7779c add missing word c2cb5d09b Tweak 'name: weight' to 'name: date' in example (#582) 5ea938ad6 Remove some Scratch 2708dcd57 Release 0.48 e375d0f05 Merge branch 'temp48' 75e36c160 releaser: Prepare repository for 0.49-DEV a6102f253 releaser: Add release notes to /docs for release of 0.48 41fc35db4 releaser: Bump versions for release of 0.48 64b9ecc74 Spell out the npm command for installing PostCSS 19e900a17 Improved Related Content doc fe21600e7 Merge commit '844aef544c19e9d8f529b4f8144e089d0982bb34' 844aef544 Squashed 'themes/gohugoioTheme/' changes from 66249819..68ddff44 069828db8 Update git.md d881d1433 Make default "related" behavior more explicit 60b9160eb Add docs for displaying 404 page on CloudFront b72ebc760 Add .gitattributes to /resources 000cf85f4 Make the pros/cons styling consistent for summaries; use desc list ebf1da97a Add note about outputStyle compressed e3338ee91 Triple backquote syntax fix 361962a7c Add one more Blogger to Hugo tool for Windows (.NET Framework 4.5) (#540) 066606a21 Fix wrong link about Mmark Syntax Document faee70757 Added exitwp-for-hugo 6b4108051 Add hugo-wrapper to starter-kits 4695dfba2 Added Utterances as Comments Alternatives. c7ba9e3e1 Correct typo beb850d9f Release 0.47.1 1cf417c8a Merge branch 'temp471' 0843bc46c releaser: Prepare repository for 0.48-DEV 8ff5c8b70 releaser: Add release notes to /docs for release of 0.47.1 e2353434d releaser: Bump versions for release of 0.47.1 ffb1300af Update development.md c22234ea5 netlify: Minify output 5b9191c56 Release 0.47 bfd92cf52 releaser: Prepare repository for 0.48-DEV ac7acf730 releaser: Add release notes to /docs for release of 0.47 b0096099d releaser: Bump versions for release of 0.47 86a7ae459 docs: Regenerate CLI docs d2c8b72bc Merge commit 'a95896878f4b4a79448b39ce93a4e0d3258b4a43' 84de7ef59 Merge commit '3a44bf182fed5f34621f450114083a6dd7e88a07' git-subtree-dir: docs git-subtree-split: 6b00298bb26b700281df28817b6556e7480cdd1e
378 lines
13 KiB
Markdown
378 lines
13 KiB
Markdown
---
|
|
title: Taxonomy Templates
|
|
# linktitle:
|
|
description: Taxonomy templating includes taxonomy list pages, taxonomy terms pages, and using taxonomies in your single page templates.
|
|
date: 2017-02-01
|
|
publishdate: 2017-02-01
|
|
lastmod: 2017-02-01
|
|
categories: [templates]
|
|
keywords: [taxonomies,metadata,front matter,terms,templates]
|
|
menu:
|
|
docs:
|
|
parent: "templates"
|
|
weight: 50
|
|
weight: 50
|
|
sections_weight: 50
|
|
draft: false
|
|
aliases: [/taxonomies/displaying/,/templates/terms/,/indexes/displaying/,/taxonomies/templates/,/indexes/ordering/, /templates/taxonomies/, /templates/taxonomy/]
|
|
toc: true
|
|
---
|
|
|
|
<!-- NOTE! Check on https://github.com/gohugoio/hugo/issues/2826 for shifting of terms' pages to .Data.Pages AND
|
|
https://discourse.gohugo.io/t/how-to-specify-category-slug/4856/15 for original discussion.-->
|
|
|
|
Hugo includes support for user-defined groupings of content called **taxonomies**. Taxonomies are classifications that demonstrate logical relationships between content. See [Taxonomies under Content Management](/content-management/taxonomies) if you are unfamiliar with how Hugo leverages this powerful feature.
|
|
|
|
Hugo provides multiple ways to use taxonomies throughout your project templates:
|
|
|
|
* Order the way content associated with a taxonomy term is displayed in a [taxonomy list template](#taxonomy-list-template)
|
|
* Order the way the terms for a taxonomy are displayed in a [taxonomy terms template](#taxonomy-terms-template)
|
|
* List a single content's taxonomy terms within a [single page template][]
|
|
|
|
## Taxonomy List Templates
|
|
|
|
Taxonomy list page templates are lists and therefore have all the variables and methods available to [list pages][lists].
|
|
|
|
### Taxonomy List Template Lookup Order
|
|
|
|
See [Template Lookup](/templates/lookup-order/).
|
|
|
|
## Taxonomy Terms Template
|
|
|
|
### Taxonomy Terms Templates Lookup Order
|
|
|
|
See [Template Lookup](/templates/lookup-order/).
|
|
|
|
### Taxonomy Methods
|
|
|
|
A Taxonomy is a `map[string]WeightedPages`.
|
|
|
|
.Get(term)
|
|
: Returns the WeightedPages for a term.
|
|
|
|
.Count(term)
|
|
: The number of pieces of content assigned to this term.
|
|
|
|
.Alphabetical
|
|
: Returns an OrderedTaxonomy (slice) ordered by Term.
|
|
|
|
.ByCount
|
|
: Returns an OrderedTaxonomy (slice) ordered by number of entries.
|
|
|
|
.Reverse
|
|
: Returns an OrderedTaxonomy (slice) in reverse order. Must be used with an OrderedTaxonomy.
|
|
|
|
### OrderedTaxonomy
|
|
|
|
Since Maps are unordered, an OrderedTaxonomy is a special structure that has a defined order.
|
|
|
|
```go
|
|
[]struct {
|
|
Name string
|
|
WeightedPages WeightedPages
|
|
}
|
|
```
|
|
|
|
Each element of the slice has:
|
|
|
|
.Term
|
|
: The Term used.
|
|
|
|
.WeightedPages
|
|
: A slice of Weighted Pages.
|
|
|
|
.Count
|
|
: The number of pieces of content assigned to this term.
|
|
|
|
.Pages
|
|
: All Pages assigned to this term. All [list methods][renderlists] are available to this.
|
|
|
|
## WeightedPages
|
|
|
|
WeightedPages is simply a slice of WeightedPage.
|
|
|
|
```go
|
|
type WeightedPages []WeightedPage
|
|
```
|
|
|
|
.Count(term)
|
|
: The number of pieces of content assigned to this term.
|
|
|
|
.Pages
|
|
: Returns a slice of pages, which then can be ordered using any of the [list methods][renderlists].
|
|
|
|
## Displaying custom metadata in Taxonomy Terms Templates
|
|
|
|
If you need to display custom metadata for each taxonomy term, you will need to create a page for that term at `/content/<TAXONOMY>/<TERM>/_index.md` and add your metadata in its front matter, [as explained in the taxonomies documentation](/content-management/taxonomies/#add-custom-meta-data-to-a-taxonomy-term). Based on the Actors taxonomy example shown there, within your taxonomy terms template, you may access your custom fields by iterating through the variable `.Pages` as such:
|
|
|
|
```go-html-template
|
|
<ul>
|
|
{{ range .Pages }}
|
|
<li>
|
|
<a href="{{ .Permalink }}">{{ .Title }}</a>
|
|
{{ .Params.wikipedia }}
|
|
</li>
|
|
{{ end }}
|
|
</ul>
|
|
```
|
|
|
|
<!-- Begin /taxonomies/ordering/ -->
|
|
|
|
## Order Taxonomies
|
|
|
|
Taxonomies can be ordered by either alphabetical key or by the number of content pieces assigned to that key.
|
|
|
|
### Order Alphabetically Example
|
|
|
|
```go-html-template
|
|
<ul>
|
|
{{ $type := .Type }}
|
|
{{ range $key, $value := .Data.Terms.Alphabetical }}
|
|
{{ $name := .Name }}
|
|
{{ $count := .Count }}
|
|
{{ with $.Site.GetPage (printf "/%s/%s" $type $name) }}
|
|
<li><a href="{{ .Permalink }}">{{ $name }}</a> {{ $count }}</li>
|
|
{{ end }}
|
|
{{ end }}
|
|
</ul>
|
|
```
|
|
|
|
### Order by Popularity Example
|
|
|
|
```go-html-template
|
|
<ul>
|
|
{{ $type := .Type }}
|
|
{{ range $key, $value := .Data.Terms.ByCount }}
|
|
{{ $name := .Name }}
|
|
{{ $count := .Count }}
|
|
{{ with $.Site.GetPage (printf "/%s/%s" $type $name) }}
|
|
<li><a href="{{ .Permalink }}">{{ $name }}</a> {{ $count }}</li>
|
|
{{ end }}
|
|
{{ end }}
|
|
</ul>
|
|
```
|
|
|
|
### Order by Least Popular Example
|
|
|
|
```go-html-template
|
|
<ul>
|
|
{{ $type := .Type }}
|
|
{{ range $key, $value := .Data.Terms.ByCount.Reverse }}
|
|
{{ $name := .Name }}
|
|
{{ $count := .Count }}
|
|
{{ with $.Site.GetPage (printf "/%s/%s" $type $name) }}
|
|
<li><a href="{{ .Permalink }}">{{ $name }}</a> {{ $count }}</li>
|
|
{{ end }}
|
|
{{ end }}
|
|
</ul>
|
|
```
|
|
|
|
<!-- [See Also Taxonomy Lists](/templates/list/) -->
|
|
|
|
## Order Content within Taxonomies
|
|
|
|
Hugo uses both `date` and `weight` to order content within taxonomies.
|
|
|
|
Each piece of content in Hugo can optionally be assigned a date. It can also be assigned a weight for each taxonomy it is assigned to.
|
|
|
|
When iterating over content within taxonomies, the default sort is the same as that used for [section and list pages]() first by weight then by date. This means that if the weights for two pieces of content are the same, than the more recent content will be displayed first. The default weight for any piece of content is 0.
|
|
|
|
### Assign Weight
|
|
|
|
Content can be assigned weight for each taxonomy that it's assigned to.
|
|
|
|
```
|
|
+++
|
|
tags = [ "a", "b", "c" ]
|
|
tags_weight = 22
|
|
categories = ["d"]
|
|
title = "foo"
|
|
categories_weight = 44
|
|
+++
|
|
Front Matter with weighted tags and categories
|
|
```
|
|
|
|
The convention is `taxonomyname_weight`.
|
|
|
|
In the above example, this piece of content has a weight of 22 which applies to the sorting when rendering the pages assigned to the "a", "b" and "c" values of the 'tag' taxonomy.
|
|
|
|
It has also been assigned the weight of 44 when rendering the 'd' category.
|
|
|
|
With this the same piece of content can appear in different positions in different taxonomies.
|
|
|
|
Currently taxonomies only support the default ordering of content which is weight -> date.
|
|
|
|
<!-- Begin /taxonomies/templates/ -->
|
|
|
|
There are two different templates that the use of taxonomies will require you to provide.
|
|
|
|
Both templates are covered in detail in the templates section.
|
|
|
|
A [list template](/templates/list/) is any template that will be used to render multiple pieces of content in a single html page. This template will be used to generate all the automatically created taxonomy pages.
|
|
|
|
A [taxonomy terms template](/templates/terms/) is a template used to
|
|
generate the list of terms for a given template.
|
|
|
|
<!-- Begin /taxonomies/displaying/ -->
|
|
|
|
There are four common ways you can display the data in your
|
|
taxonomies in addition to the automatic taxonomy pages created by hugo
|
|
using the [list templates](/templates/list/):
|
|
|
|
1. For a given piece of content, you can list the terms attached
|
|
2. For a given piece of content, you can list other content with the same
|
|
term
|
|
3. You can list all terms for a taxonomy
|
|
4. You can list all taxonomies (with their terms)
|
|
|
|
## Display a Single Piece of Content's Taxonomies
|
|
|
|
Within your content templates, you may wish to display the taxonomies that piece of content is assigned to.
|
|
|
|
Because we are leveraging the front matter system to define taxonomies for content, the taxonomies assigned to each content piece are located in the usual place (i.e., `.Params.<TAXONOMYPLURAL>`).
|
|
|
|
### Example: List Tags in a Single Page Template
|
|
|
|
```go-html-template
|
|
{{ $taxo := "tags" }} <!-- Use the plural form here -->
|
|
<ul id="{{ $taxo }}">
|
|
{{ range .Param $taxo }}
|
|
{{ $name := . }}
|
|
{{ with $.Site.GetPage (printf "/%s/%s" $taxo $name) }}
|
|
<li><a href="{{ .Permalink }}">{{ $name }}</a></li>
|
|
{{ end }}
|
|
{{ end }}
|
|
</ul>
|
|
```
|
|
|
|
If you want to list taxonomies inline, you will have to take care of optional plural endings in the title (if multiple taxonomies), as well as commas. Let's say we have a taxonomy "directors" such as `directors: [ "Joel Coen", "Ethan Coen" ]` in the TOML-format front matter.
|
|
|
|
To list such taxonomies, use the following:
|
|
|
|
### Example: Comma-delimit Tags in a Single Page Template
|
|
|
|
```go-html-template
|
|
{{ $taxo := "directors" }} <!-- Use the plural form here -->
|
|
{{ with .Param $taxo }}
|
|
<strong>Director{{ if gt (len .) 1 }}s{{ end }}:</strong>
|
|
{{ range $index, $director := . }}
|
|
{{- if gt $index 0 }}, {{ end -}}
|
|
{{ with $.Site.GetPage (printf "/%s/%s" $taxo $director) -}}
|
|
<a href="{{ .Permalink }}">{{ $director }}</a>
|
|
{{- end -}}
|
|
{{- end -}}
|
|
{{ end }}
|
|
```
|
|
|
|
Alternatively, you may use the [delimit template function][delimit] as a shortcut if the taxonomies should just be listed with a separator. See {{< gh 2143 >}} on GitHub for discussion.
|
|
|
|
## List Content with the Same Taxonomy Term
|
|
|
|
If you are using a taxonomy for something like a series of posts, you can list individual pages associated with the same taxonomy. This is also a quick and dirty method for showing related content:
|
|
|
|
### Example: Showing Content in Same Series
|
|
|
|
```go-html-template
|
|
<ul>
|
|
{{ range .Site.Taxonomies.series.golang }}
|
|
<li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a></li>
|
|
{{ end }}
|
|
</ul>
|
|
```
|
|
|
|
## List All content in a Given taxonomy
|
|
|
|
This would be very useful in a sidebar as “featured content”. You could even have different sections of “featured content” by assigning different terms to the content.
|
|
|
|
### Example: Grouping "Featured" Content
|
|
|
|
```go-html-template
|
|
<section id="menu">
|
|
<ul>
|
|
{{ range $key, $taxonomy := .Site.Taxonomies.featured }}
|
|
<li>{{ $key }}</li>
|
|
<ul>
|
|
{{ range $taxonomy.Pages }}
|
|
<li hugo-nav="{{ .RelPermalink}}"><a href="{{ .Permalink}}">{{ .LinkTitle }}</a></li>
|
|
{{ end }}
|
|
</ul>
|
|
{{ end }}
|
|
</ul>
|
|
</section>
|
|
```
|
|
|
|
## Render a Site's Taxonomies
|
|
|
|
If you wish to display the list of all keys for your site's taxonomy, you can retrieve them from the [`.Site` variable][sitevars] available on every page.
|
|
|
|
This may take the form of a tag cloud, a menu, or simply a list.
|
|
|
|
The following example displays all terms in a site's tags taxonomy:
|
|
|
|
### Example: List All Site Tags {#example-list-all-site-tags}
|
|
|
|
```go-html-template
|
|
<ul id="all-tags">
|
|
{{ range $name, $taxonomy := .Site.Taxonomies.tags }}
|
|
{{ with $.Site.GetPage (printf "/tags/%s" $name) }}
|
|
<li><a href="{{ .Permalink }}">{{ $name }}</a></li>
|
|
{{ end }}
|
|
{{ end }}
|
|
</ul>
|
|
```
|
|
|
|
### Example: List All Taxonomies, Terms, and Assigned Content
|
|
|
|
This example will list all taxonomies and their terms, as well as all the content assigned to each of the terms.
|
|
|
|
{{< code file="layouts/partials/all-taxonomies.html" download="all-taxonomies.html" download="all-taxonomies.html" >}}
|
|
<section>
|
|
<ul id="all-taxonomies">
|
|
{{ range $taxonomy_term, $taxonomy := .Site.Taxonomies }}
|
|
{{ with $.Site.GetPage (printf "/%s" $taxonomy_term) }}
|
|
<li><a href="{{ .Permalink }}">{{ $taxonomy_term }}</a>
|
|
<ul>
|
|
{{ range $key, $value := $taxonomy }}
|
|
<li>{{ $key }}</li>
|
|
<ul>
|
|
{{ range $value.Pages }}
|
|
<li hugo-nav="{{ .RelPermalink}}">
|
|
<a href="{{ .Permalink}}">{{ .LinkTitle }}</a>
|
|
</li>
|
|
{{ end }}
|
|
</ul>
|
|
{{ end }}
|
|
</ul>
|
|
</li>
|
|
{{ end }}
|
|
{{ end }}
|
|
</ul>
|
|
</section>
|
|
{{< /code >}}
|
|
|
|
## `.Site.GetPage` for Taxonomies
|
|
|
|
Because taxonomies are lists, the [`.GetPage` function][getpage] can be used to get all the pages associated with a particular taxonomy term using a terse syntax. The following ranges over the full list of tags on your site and links to each of the individual taxonomy pages for each term without having to use the more fragile URL construction of the ["List All Site Tags" example above]({{< relref "#example-list-all-site-tags" >}}):
|
|
|
|
{{< code file="links-to-all-tags.html" >}}
|
|
{{ $taxo := "tags" }}
|
|
<ul class="{{ $taxo }}">
|
|
{{ with ($.Site.GetPage (printf "/%s" $taxo)) }}
|
|
{{ range .Pages }}
|
|
<li><a href="{{ .Permalink }}">{{ .Title}}</a></li>
|
|
{{ end }}
|
|
{{ end }}
|
|
</ul>
|
|
{{< /code >}}
|
|
|
|
<!-- TODO: ### `.Site.GetPage` Taxonomy List Example -->
|
|
|
|
<!-- TODO: ### `.Site.GetPage` Taxonomy Terms Example -->
|
|
|
|
|
|
[delimit]: /functions/delimit/
|
|
[getpage]: /functions/getpage/
|
|
[lists]: /templates/lists/
|
|
[renderlists]: /templates/lists/
|
|
[single page template]: /templates/single-page-templates/
|
|
[sitevars]: /variables/site/
|