Hugo themes Doit 合并 tags , categories 为检索页

总觉得 tags, categories 等页面可以合并成为一页。这样检索起来更方便一些。成果 https://www.ftls.xyz/retrieval/

config.toml

1
2
3
4
5
6
7
# 用于分类的设置
[taxonomies]
author = "authors"
category = "categories"
tag = "tags"
series = "series"
retrieval = "retrieval"

尝试使用 yaml 中的锚点和引用。

1
2
3
4
5
tags: &tag []
categories: &cat []
retrieval:
 tag: *tag
 cat: *cat

很遗憾,失败了。这在单页面上,也就是说具体页面。可以成功,然而在检索页却并不是可以成功的。 https://www.ftls.xyz/retrieval/ 不能获取到正确数据。

使用如下在 terms.html 中获取自定义分类的具体内容。

1
{{- $terms := $.Site.Taxonomies.series.ByCount -}}

这里使用的是 Doit 的主题。大家可以借鉴一下看看大致原理。知道有这个写法,大致就知道怎么写的了。

themes\DoIt\layouts\taxonomy\terms.html end 前新增的内容

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	{{- /* Sum Retrieval Page */ -}}
        {{- else if eq $taxonomies "retrieval" -}}
            <h3><a href="/tags/">>标签</a>&nbsp;&nbsp;<a href="/series/">>系列</a>&nbsp;&nbsp;<a href="/categories/">>分类</a></h3>
            <h2><a href="/tags/">标签</a></h2>
            <div class="tag-cloud-tags">
                {{- range $.Site.Taxonomies.tags.ByCount -}}
                    <a href="{{ .Page.RelPermalink }}">{{ .Page.Title }} <sup>{{ .Count }}</sup></a>
                {{- end -}}
            </div>
            <h2><a href="/series/">系列</a></h2>
            {{- $terms := $.Site.Taxonomies.series.ByCount -}}
            <div>
                {{- range $terms -}}
                    {{- $term := .Term -}}
                    {{- $pages := .Pages -}}
                    {{- $type := "series" -}}
                    {{-  with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
                    <div class="card-item">
                        <div class="card-item-wrapper">
                            <h3 class="card-item-title">
                                <a href="{{ .RelPermalink }}">
                                    <i class="far fa-folder fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                                </a>
                            </h3>
                            {{- range first 5 $pages -}}
                                <article class="archive-item">
                                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                                        {{- .Title -}}
                                    </a>
                                </article>
                            {{- end -}}
                            {{- if gt (len $pages) 5 -}}
                                <span class="more-post">
                                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                                </span>
                            {{- end -}}
                        </div>
                    </div>
                    {{- end -}}
                {{- end -}}
            </div>

            <h2><a href="/categories/">分类</a></h2>
	        {{- $terms := $.Site.Taxonomies.categories.ByCount -}}
            <div>
                {{- range $terms -}}
                    {{- $term := .Term -}}
                    {{- $pages := .Pages -}}
		    {{- $type := "categories" -}}
                    {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
                    <div class="card-item">
                        <div class="card-item-wrapper">
                            <h3 class="card-item-title">
                                <a href="{{ .RelPermalink }}">
                                    <i class="far fa-folder fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                                </a>
                            </h3>
                            {{- range first 5 $pages -}}
                                <article class="archive-item">
                                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                                        {{- .Title -}}
                                    </a>
                                </article>
                            {{- end -}}
                            {{- if gt (len $pages) 5 -}}
                                <span class="more-post">
                                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                                </span>
                            {{- end -}}
                        </div>
                    </div>
                    {{- end -}}
                {{- end -}}
            </div>

加入之后的整体 terms.html ,也可以直接放到 layouts\taxonomy\terms.html 下,这个文件夹优先级比主题高。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
{{- define "title" -}}
    {{- .Params.Title | default (T .Data.Plural) | default .Data.Plural | dict "Some" | T "allSome" }} - {{ .Site.Title -}}
{{- end -}}

{{- define "content" -}}
    {{- $taxonomies := .Data.Plural -}}
    {{- $type := .Type -}}

    <div class="page archive">
        {{- /* Title */ -}}
        <h2 class="single-title animate__animated animate__pulse animate__faster">
            {{- .Params.Title | default (T $taxonomies) | default $taxonomies | dict "Some" | T "allSome" -}}
        </h2>

        {{- /* Categories Page */ -}}
        {{- if eq $taxonomies "categories" -}}
            {{- $terms := .Data.Terms.ByCount -}}
            <div class="categories-card">
                {{- range $terms -}}
                    {{- $term := .Term -}}
                    {{- $pages := .Pages -}}
                    {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
                    <div class="card-item">
                        <div class="card-item-wrapper">
                            <h3 class="card-item-title">
                                <a href="{{ .RelPermalink }}">
                                    <i class="far fa-folder fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                                </a>
                            </h3>
                            {{- range first 5 $pages -}}
                                <article class="archive-item">
                                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                                        {{- .Title -}}
                                    </a>
                                </article>
                            {{- end -}}
                            {{- if gt (len $pages) 5 -}}
                                <span class="more-post">
                                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                                </span>
                            {{- end -}}
                        </div>
                    </div>
                    {{- end -}}
                {{- end -}}
            </div>

        {{- /* Series Page */ -}}
        {{- else if eq $taxonomies "series" -}}
            {{- $terms := .Data.Terms.ByCount -}}
            <div class="series-card">
                {{- range $terms -}}
                    {{- $term := .Term -}}
                    {{- $pages := .Pages -}}
                    {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
                    <div class="card-item">
                        <div class="card-item-wrapper">
                            <h3 class="card-item-title">
                                <a href="{{ .RelPermalink }}">
                                    <i class="far fa-folder fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                                </a>
                            </h3>
                            {{- range first 5 $pages -}}
                                <article class="archive-item">
                                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                                        {{- .Title -}}
                                    </a>
                                </article>
                            {{- end -}}
                            {{- if gt (len $pages) 5 -}}
                                <span class="more-post">
                                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                                </span>
                            {{- end -}}
                        </div>
                    </div>
                    {{- end -}}
                {{- end -}}
            </div>
            
        {{- /* Author Page */ -}}
        {{- else if eq $taxonomies "authors" -}}
            {{- $terms := .Data.Terms.Alphabetical -}}
            <div class="author-card">
                {{- range $terms -}}
                    {{- $term := .Term -}}
                    {{- $pages := .Pages -}}
                    {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
                    <div class="card-item">
                        <div class="card-item-wrapper">
                            <h3 class="card-item-title">
                                <a href="{{ .RelPermalink }}">
                                    <i class="fas fa-user-circle fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                                </a>
                            </h3>
                            {{- range first 5 $pages -}}
                                <article class="archive-item">
                                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                                        {{- .Title -}}
                                    </a>
                                </article>
                            {{- end -}}
                            {{- if gt (len $pages) 5 -}}
                                <span class="more-post">
                                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                                </span>
                            {{- end -}}
                        </div>
                    </div>
                    {{- end -}}
                {{- end -}}
            </div>

        {{- /* Tag Cloud Page */ -}}
        {{- else if eq $taxonomies "tags" -}}
            <div class="tag-cloud-tags">
                {{- range $.Site.Taxonomies.tags.ByCount -}}
                    <a href="{{ .Page.RelPermalink }}">{{ .Page.Title }} <sup>{{ .Count }}</sup></a>
                {{- end -}}
            </div>

	{{- /* Sum Retrieval Page */ -}}
        {{- else if eq $taxonomies "retrieval" -}}
            <h3><a href="/tags/">>标签</a>&nbsp;&nbsp;<a href="/series/">>系列</a>&nbsp;&nbsp;<a href="/categories/">>分类</a></h3>
            <h2><a href="/tags/">标签</a></h2>
            <div class="tag-cloud-tags">
                {{- range $.Site.Taxonomies.tags.ByCount -}}
                    <a href="{{ .Page.RelPermalink }}">{{ .Page.Title }} <sup>{{ .Count }}</sup></a>
                {{- end -}}
            </div>
            <h2><a href="/series/">系列</a></h2>
            {{- $terms := $.Site.Taxonomies.series.ByCount -}}
            <div>
                {{- range $terms -}}
                    {{- $term := .Term -}}
                    {{- $pages := .Pages -}}
                    {{- $type := "series" -}}
                    {{-  with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
                    <div class="card-item">
                        <div class="card-item-wrapper">
                            <h3 class="card-item-title">
                                <a href="{{ .RelPermalink }}">
                                    <i class="far fa-folder fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                                </a>
                            </h3>
                            {{- range first 5 $pages -}}
                                <article class="archive-item">
                                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                                        {{- .Title -}}
                                    </a>
                                </article>
                            {{- end -}}
                            {{- if gt (len $pages) 5 -}}
                                <span class="more-post">
                                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                                </span>
                            {{- end -}}
                        </div>
                    </div>
                    {{- end -}}
                {{- end -}}
            </div>

            <h2><a href="/categories/">分类</a></h2>
	        {{- $terms := $.Site.Taxonomies.categories.ByCount -}}
            <div>
                {{- range $terms -}}
                    {{- $term := .Term -}}
                    {{- $pages := .Pages -}}
		    {{- $type := "categories" -}}
                    {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
                    <div class="card-item">
                        <div class="card-item-wrapper">
                            <h3 class="card-item-title">
                                <a href="{{ .RelPermalink }}">
                                    <i class="far fa-folder fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                                </a>
                            </h3>
                            {{- range first 5 $pages -}}
                                <article class="archive-item">
                                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                                        {{- .Title -}}
                                    </a>
                                </article>
                            {{- end -}}
                            {{- if gt (len $pages) 5 -}}
                                <span class="more-post">
                                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                                </span>
                            {{- end -}}
                        </div>
                    </div>
                    {{- end -}}
                {{- end -}}
            </div>
        {{- end -}}
    </div>
{{- end -}}