HUGO
Menu
GitHub 87548 stars Mastodon

Taxonomies

返回一个数据结构,包含站点的分类法对象、每个分类法对象中的术语以及分配给这些术语的页面。

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"
         ]
      }
   ]
}

例如,在书评网站上,您可以创建两个分类法:一个用于流派,另一个用于作者。

使用此站点配置:

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"
         ]
      }
   ]
}

要列出"suspense"类型的书籍:

<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>

Hugo 的分类法系统功能强大,允许您对内容进行分类并在页面之间创建关系。

请参阅 分类法 部分以获取完整解释和示例。

示例

列出具有相同分类法术语的内容

如果您使用分类法来标记一系列帖子之类的内容,可以列出与同一术语关联的各个页面。例如:

<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>

渲染站点的分类法

以下示例显示站点 tags 分类法中的所有术语:

<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 }}

Last updated: January 1, 0001
Improve this page