Count
返回给定术语被分配到的加权页面数量。
Syntax
TAXONOMY.Count TERM
Returns
int
Taxonomy 对象上的 Count 方法返回给定 术语 被分配到的 加权页面 数量。
在使用 Taxonomy 方法之前,我们需要获取一个 Taxonomy 对象。
获取分类法对象
考虑此站点配置:
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要在任何模板中获取 “genres” Taxonomy 对象,请在 Site 对象上使用 Taxonomies 方法。
{{ $taxonomyObject := .Site.Taxonomies.genres }}在使用 分类法 模板渲染其页面时获取 “genres” Taxonomy 对象,请在页面的 Data 对象上使用 Terms 方法:
layouts/taxonomy.html
{{ $taxonomyObject := .Data.Terms }}要检查数据结构:
<pre>{{ debug.Dump $taxonomyObject }}</pre>虽然 Alphabetical 和 ByCount 方法提供了更好的数据结构用于遍历分类法,但您可以直接从 Taxonomy 对象按术语渲染加权页面:
{{ range $term, $weightedPages := $taxonomyObject }}
<h2><a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a></h2>
<ul>
{{ range $weightedPages }}
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
{{ end }}
</ul>
{{ end }}在上面的示例中,第一个锚元素是指向术语页面的链接。
统计加权页面
现在我们已经获取了"genres"分类法对象,让我们统计"suspense"术语被分配到的加权页面数量:
{{ $taxonomyObject.Count "suspense" }} → 3