HUGO
Menu
GitHub 87548 stars Mastodon

Hugo 分类法

Hugo 包含对用户定义分类法的支持。

什么是分类法?

Hugo 包含对称为 taxonomies(分类法)的用户定义内容分组的支持。分类法是内容之间逻辑关系的分类。

定义

Taxonomy(分类法)
可用于分类内容的分类
Term(术语)
分类法中的键
Value(值)
分配给术语的内容片段

示例分类法:电影网站

假设你正在制作一个关于电影的网站。你可能希望包含以下分类法:

  • Actors(演员)
  • Directors(导演)
  • Studios(制片公司)
  • Genre(类型)
  • Year(年份)
  • Awards(奖项)

然后,在每部电影中,你将指定每个分类法的术语(即,在每部电影内容文件的 front matter 中)。根据这些术语,Hugo 将自动为每个演员、导演、制片公司、类型、年份和奖项创建页面,每个页面列出所有匹配该特定演员、导演、制片公司、类型、年份和奖项的电影。

电影分类法组织

继续以电影网站为例,以下从分类法的角度演示了内容关系:

Actor                    <- 分类法
    Bruce Willis         <- 术语
        The Sixth Sense  <- 值
        Unbreakable      <- 值
        Moonrise Kingdom <- 值
    Samuel L. Jackson    <- 术语
        Unbreakable      <- 值
        The Avengers     <- 值
        xXx              <- 值

从内容的角度来看,关系将显示不同,尽管使用的数据和标签相同:

Unbreakable                 <- 值
    Actors                  <- 分类法
        Bruce Willis        <- 术语
        Samuel L. Jackson   <- 术语
    Director                <- 分类法
        M. Night Shyamalan  <- 术语
    ...
Moonrise Kingdom            <- 值
    Actors                  <- 分类法
        Bruce Willis        <- 术语
        Bill Murray         <- 术语
    Director                <- 分类法
        Wes Anderson        <- 术语
    ...

默认目标

当使用分类法时,Hugo 将自动创建列出所有分类法术语的页面,以及包含与每个术语关联的内容列表的单独页面。例如,在配置中声明并在内容 front matter 中使用的 categories 分类法将创建以下页面:

  • example.com/categories/ 的单个页面,列出分类法中的所有术语
  • 每个术语的单独分类法列表页面(例如,/categories/development/),显示在任何内容文件的 front matter 中标记为该分类法的所有页面列表

配置

请参阅 configure taxonomies

为内容分配术语

要为页面分配一个或多个术语,使用分类法的复数名称创建 front matter 字段,然后将术语添加到相应的数组。例如:

---
categories:
- Category A
- Category B
tags:
- Tag A
- Tag B
title: Example
---
+++
categories = ['Category A', 'Category B']
tags = ['Tag A', 'Tag B']
title = 'Example'
+++
{
   "categories": [
      "Category A",
      "Category B"
   ],
   "tags": [
      "Tag A",
      "Tag B"
   ],
   "title": "Example"
}

分类权重

taxonomic weight(分类权重)是在 front matter 中定义且对每个分类法唯一?weight(权重),用于确?Taxonomy 对象中包含的页面集合的排序顺序

使用名为 [taxonomy_name]_weight 的 front matter 键分配分类权重。

---
tags:
- chemistry
- science
tags_weight: 1000
title: Organic Chemistry
weight: 10
---
+++
tags = ['chemistry', 'science']
tags_weight = 1000
title = 'Organic Chemistry'
weight = 10
+++
{
   "tags": [
      "chemistry",
      "science"
   ],
   "tags_weight": 1000,
   "title": "Organic Chemistry",
   "weight": 10
}

使用上面的 front matter,“Organic Chemistry” 页面将在 section 和首页上浮动到列表顶部,并且它将在"chemistry"和"science"术语页面上沉到列表底部。

元数据

通过在 content 目录中创建相应的 branch bundle 来显示每个术语的元数据。

例如,创建"authors"分类法:

taxonomies:
  author: authors
[taxonomies]
  author = 'authors'
{
   "taxonomies": {
      "author": "authors"
   }
}

然后为每个术语创建带有一个 branch bundle 的内容:

content/
└── authors/
    ├── jsmith/
    │   ├── _index.md
    │   └── portrait.jpg
    └── rjones/
        ├── _index.md
        └── portrait.jpg

然后为每个术语页面添加 front matter:

---
affiliation: University of Chicago
title: John Smith
---
+++
affiliation = 'University of Chicago'
title = 'John Smith'
+++
{
   "affiliation": "University of Chicago",
   "title": "John Smith"
}

然后创建特定于"authors"分类法的 taxonomy 模板:

layouts/authors/taxonomy.html
{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ .Content }}
  {{ range .Data.Terms.Alphabetical }}
    <h2><a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a></h2>
    <p>Affiliation: {{ .Page.Params.Affiliation }}</p>
    {{ with .Page.Resources.Get "portrait.jpg" }}
      {{ with .Fill "100x100" }}
        <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="portrait">
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}

在上面的示例中,我们列出每个作者,包括他们的隶属关系和肖像。

或者创建特定于"authors"分类法的 term 模板:

layouts/authors/term.html
{{ define "main" }}
  <h1>{{ .Title }}</h1>
  <p>Affiliation: {{ .Params.affiliation }}</p>
  {{ with .Resources.Get "portrait.jpg" }}
    {{ with .Fill "100x100" }}
      <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="portrait">
    {{ end }}
  {{ end }}
  {{ .Content }}
  {{ range .Pages }}
    <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
  {{ end }}
{{ end }}

在上面的示例中,我们显示作者,包括他们的隶属关系和肖像,然后列出关联的内容。


Last updated: January 1, 0001
Improve this page