分类法
Syntax
SITE.Taxonomies
Returns
page.TaxonomyList
从概念上讲, Site
对象上的 Taxonomies
方法返回的数据结构如下:
taxonomy a:
- term 1:
- page 1
- page 2
- term 2:
- page 1
taxonomy b:
- term 1:
- page 2
- term 2:
- page 1
- page 2
[['taxonomy a']]
'term 1' = ['page 1', 'page 2']
[['taxonomy a']]
'term 2' = ['page 1']
[['taxonomy b']]
'term 1' = ['page 2']
[['taxonomy b']]
'term 2' = ['page 1', 'page 2']
{
"taxonomy a": [
{
"term 1": [
"page 1",
"page 2"
]
},
{
"term 2": [
"page 1"
]
}
],
"taxonomy b": [
{
"term 1": [
"page 2"
]
},
{
"term 2": [
"page 1",
"page 2"
]
}
]
}
例如,在一个书评网站上,你可能会创建两个分类法;一个用于类型,另一个用于作者。
使用此站点配置:
hugo.
taxonomies:
author: authors
genre: genres
[taxonomies]
author = 'authors'
genre = 'genres'
{
"taxonomies": {
"author": "authors",
"genre": "genres"
}
}
以及此内容结构:
content/
├── books/
│ ├── and-then-there-were-none.md --> genres: suspense
│ ├── death-on-the-nile.md --> genres: suspense
│ └── jamaica-inn.md --> genres: suspense, romance
│ └── pride-and-prejudice.md --> genres: romance
└── _index.md
从概念上讲,分类法数据结构如下所示:
authors:
- achristie:
- And Then There Were None
- Death on the Nile
- ddmaurier:
- Jamaica Inn
- jausten:
- Pride and Prejudice
genres:
- suspense:
- And Then There Were None
- Death on the Nile
- Jamaica Inn
- romance:
- Jamaica Inn
- Pride and Prejudice
[[authors]]
achristie = ['And Then There Were None', 'Death on the Nile']
[[authors]]
ddmaurier = ['Jamaica Inn']
[[authors]]
jausten = ['Pride and Prejudice']
[[genres]]
suspense = ['And Then There Were None', 'Death on the Nile', 'Jamaica Inn']
[[genres]]
romance = ['Jamaica Inn', 'Pride and Prejudice']
{
"authors": [
{
"achristie": [
"And Then There Were None",
"Death on the Nile"
]
},
{
"ddmaurier": [
"Jamaica Inn"
]
},
{
"jausten": [
"Pride and Prejudice"
]
}
],
"genres": [
{
"suspense": [
"And Then There Were None",
"Death on the Nile",
"Jamaica Inn"
]
},
{
"romance": [
"Jamaica Inn",
"Pride and Prejudice"
]
}
]
}
要列出“悬念”书籍:
<ul>
{{ range .Site.Taxonomies.genres.suspense }}
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
{{ end }}
</ul>
Hugo将其呈现为:
<ul>
<li><a href="/books/and-then-there-were-none/">And Then There Were None</a></li>
<li><a href="/books/death-on-the-nile/">Death on the Nile</a></li>
<li><a href="/books/jamaica-inn/">Jamaica Inn</a></li>
</ul>
示例
列出具有相同分类术语的内容
如果您正在使用分类法来表示一系列帖子,则可以列出与同一术语关联的各个页面。例如:
<ul>
{{ range .Site.Taxonomies.series.golang }}
<li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a></li>
{{ end }}
</ul>
列出给定分类法中的所有内容
这在侧边栏中作为“特色内容”非常有用。您甚至可以通过为内容分配不同的术语来拥有不同的“特色内容”部分。
<section id="menu">
<ul>
{{ range $term, $taxonomy := .Site.Taxonomies.featured }}
<li>{{ $term }}</li>
<ul>
{{ range $taxonomy.Pages }}
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
{{ end }}
</ul>
{{ end }}
</ul>
</section>
呈现站点的分类法
以下示例显示站点标签分类法中的所有术语:
<ul>
{{ range .Site.Taxonomies.tags }}
<li><a href="{{ .Page.Permalink }}">{{ .Page.Title }}</a> {{ .Count }}</li>
{{ end }}
</ul>
此示例将列出所有分类法及其术语,以及分配给每个术语的所有内容。
layouts/partials/all-taxonomies.html
{{ with .Site.Taxonomies }}
{{ $numberOfTerms := 0 }}
{{ range $taxonomy, $terms := . }}
{{ $numberOfTerms = len . | add $numberOfTerms }}
{{ end }}
{{ if gt $numberOfTerms 0 }}
<ul>
{{ range $taxonomy, $terms := . }}
{{ with $terms }}
<li>
<a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a>
<ul>
{{ range $term, $weightedPages := . }}
<li>
<a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a>
<ul>
{{ range $weightedPages }}
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
{{ end }}
</ul>
</li>
{{ end }}
</ul>
</li>
{{ end }}
{{ end }}
</ul>
{{ end }}
{{ end }}