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