# Hugo themes Doit 合并 tags , categories 为检索页
总觉得 tags, categories 等页面可以合并成为一页。这样检索起来更方便一些。成果 https://www.ftls.xyz/retrieval/
## 初始配置
config.toml
```toml
# 用于分类的设置
[taxonomies]
author = "authors"
category = "categories"
tag = "tags"
series = "series"
retrieval = "retrieval"
```
## 失败尝试
尝试使用 yaml 中的锚点和引用。
如
```yaml
tags: &tag []
categories: &cat []
retrieval:
tag: *tag
cat: *cat
```
很遗憾,失败了。这在单页面上,也就是说具体页面。可以成功,然而在检索页却并不是可以成功的。 https://www.ftls.xyz/retrieval/ 不能获取到正确数据。
## 正确尝试
使用如下在 terms.html 中获取自定义分类的具体内容。
```
{{- $terms := $.Site.Taxonomies.series.ByCount -}}
```
这里使用的是 Doit 的主题。大家可以借鉴一下看看大致原理。知道有这个写法,大致就知道怎么写的了。
themes\DoIt\layouts\taxonomy\terms.html end 前新增的内容
```html
{{- /* Sum Retrieval Page */ -}}
{{- else if eq $taxonomies "retrieval" -}}
{{- /* Title */ -}}
{{- .Params.Title | default (T $taxonomies) | default $taxonomies | dict "Some" | T "allSome" -}}
{{- /* Categories Page */ -}}
{{- if eq $taxonomies "categories" -}}
{{- $terms := .Data.Terms.ByCount -}}
{{- range $terms -}}
{{- $term := .Term -}}
{{- $pages := .Pages -}}
{{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
{{- end -}}
{{- end -}}
{{- /* Series Page */ -}}
{{- else if eq $taxonomies "series" -}}
{{- $terms := .Data.Terms.ByCount -}}
{{- range $terms -}}
{{- $term := .Term -}}
{{- $pages := .Pages -}}
{{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
{{- end -}}
{{- end -}}
{{- /* Author Page */ -}}
{{- else if eq $taxonomies "authors" -}}
{{- $terms := .Data.Terms.Alphabetical -}}
{{- range $terms -}}
{{- $term := .Term -}}
{{- $pages := .Pages -}}
{{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
{{- end -}}
{{- end -}}
{{- /* Tag Cloud Page */ -}}
{{- else if eq $taxonomies "tags" -}}
{{- /* Sum Retrieval Page */ -}}
{{- else if eq $taxonomies "retrieval" -}}
{{- $terms := $.Site.Taxonomies.series.ByCount -}}
{{- range $terms -}}
{{- $term := .Term -}}
{{- $pages := .Pages -}}
{{- $type := "series" -}}
{{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
{{- end -}}
{{- end -}}
{{- $terms := $.Site.Taxonomies.categories.ByCount -}}
{{- range $terms -}}
{{- $term := .Term -}}
{{- $pages := .Pages -}}
{{- $type := "categories" -}}
{{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
```
## 优化
2024-04-21 优化,使用 Hugo 模板查找顺序。
content/retrieval.md
```md
---
title: 检索
layout: retrieval
---
```
layouts/page/retrieval.html
```html
{{- define "title" -}}
检索 - {{ .Site.Title -}}
{{- end -}}
{{- define "content" -}}
{{- /* Sum Retrieval Page */ -}}
{{- $terms := $.Site.Taxonomies.series.ByCount -}}
{{- range $terms -}}
{{- $term := .Term -}}
{{- $pages := .Pages -}}
{{- $type := "series" -}}
{{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
{{- end -}}
{{- end -}}
{{- $terms := $.Site.Taxonomies.categories.ByCount -}}
{{- range $terms -}}
{{- $term := .Term -}}
{{- $pages := .Pages -}}
{{- $type := "categories" -}}
{{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
{{- end -}}
{{- end -}}
{{ $uncategorized := where .Site.RegularPages "Type" "!=" "page" }}
{{- $pages := slice -}}
{{- range $uncategorized -}}
{{ $cat := (printf "%v" .Params.categories ) }}
{{ if ne $cat "
" | eq $cat "[]" }}
{{ $pages = $pages | append . }}
{{- end -}}
{{- end -}}
{{- end -}}
```
assets/css/_custom.scss
```scss
// 检索页 hr
.retrieval {
.archive-title {
display: flex;
align-items: center;
gap: 0.5rem;
}
hr {
margin: 0.5rem 0;
position: relative;
border-top: 3px dashed $global-border-color;
border-bottom: none;
[theme="dark"] & {
border-top: 3px dashed $global-border-color-dark;
}
[theme="black"] & {
border-top: 3px dashed $global-border-color-black;
}
}
}
```