需求:在搜索页展示系列列表

以下是实现步骤

  1. 编辑搜索页面模板
./layouts/_default/search.html
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{{- if not (.Param "hideSeries")}}
{{- $taxonomies := .Site.Taxonomies.series }}
{{- if gt (len $taxonomies) 0 }}
<h2 style="margin-top: 32px">{{- (.Param "seriesTitle") | default "series" }}</h2>
<ul class="terms-tags">
    {{- range $name, $value := $taxonomies }}
    {{- $count := .Count }}
    {{- with site.GetPage (printf "/series/%s" $name) }}
    <li>
        <a href="{{ .Permalink }}">{{ .Name }} <sup><strong><sup>{{ $count }}</sup></strong></sup> </a>
    </li>
    {{- end }}
    {{- end }}
</ul>
{{- end }}
{{- end }}
{{- end }}{{/* end main */}}  <!-- 在最后一行前加入上面的代码 -->
  1. 使用姿势
./config.yml
1
2
3
4
# 设置文章分类 https://gohugo.io/content-management/taxonomies
taxonomies:
    tag: tags
    series: series

设置文章所属系列,比如本篇是这样:

1
2
3
4
5
6
7
---
title: "PaperMod 搜索页展示系列列表"
date: 2022-06-10
draft: false
tags: ["hugo", "paper-modx"]
series: ["PaperModx 定制搜索页"]
---

可通过以下配置隐藏系列列表

./content/search.md
1
2
3
4
5
6
7
---
title: "搜索"
layout: "search"
# 是否隐藏系列
hideSeries: false
seriesTitle: 系列 
---