# 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" -}}

>标签  >系列  >分类

标签

{{- range $.Site.Taxonomies.tags.ByCount -}} {{ .Page.Title }} {{ .Count }} {{- end -}}

系列

{{- $terms := $.Site.Taxonomies.series.ByCount -}}
{{- range $terms -}} {{- $term := .Term -}} {{- $pages := .Pages -}} {{- $type := "series" -}} {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}

 {{ .Page.Title }} {{- len $pages -}}

{{- range first 5 $pages -}} {{- end -}} {{- if gt (len $pages) 5 -}} {{ T "more" }} >> {{- end -}}
{{- end -}} {{- end -}}

分类

{{- $terms := $.Site.Taxonomies.categories.ByCount -}}
{{- range $terms -}} {{- $term := .Term -}} {{- $pages := .Pages -}} {{- $type := "categories" -}} {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}

 {{ .Page.Title }} {{- len $pages -}}

{{- range first 5 $pages -}} {{- end -}} {{- if gt (len $pages) 5 -}} {{ T "more" }} >> {{- end -}}
{{- end -}} {{- end -}}
``` 加入之后的整体 terms.html ,也可以直接放到 layouts\taxonomy\terms.html 下,这个文件夹优先级比主题高。 ```html {{- 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 -}}
{{- /* 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) -}}

 {{ .Page.Title }} {{- len $pages -}}

{{- range first 5 $pages -}} {{- end -}} {{- if gt (len $pages) 5 -}} {{ T "more" }} >> {{- end -}}
{{- 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) -}}

 {{ .Page.Title }} {{- len $pages -}}

{{- range first 5 $pages -}} {{- end -}} {{- if gt (len $pages) 5 -}} {{ T "more" }} >> {{- end -}}
{{- 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) -}}

 {{ .Page.Title }} {{- len $pages -}}

{{- range first 5 $pages -}} {{- end -}} {{- if gt (len $pages) 5 -}} {{ T "more" }} >> {{- end -}}
{{- end -}} {{- end -}}
{{- /* Tag Cloud Page */ -}} {{- else if eq $taxonomies "tags" -}}
{{- range $.Site.Taxonomies.tags.ByCount -}} {{ .Page.Title }} {{ .Count }} {{- end -}}
{{- /* Sum Retrieval Page */ -}} {{- else if eq $taxonomies "retrieval" -}}

>标签  >系列  >分类

标签

{{- range $.Site.Taxonomies.tags.ByCount -}} {{ .Page.Title }} {{ .Count }} {{- end -}}

系列

{{- $terms := $.Site.Taxonomies.series.ByCount -}}
{{- range $terms -}} {{- $term := .Term -}} {{- $pages := .Pages -}} {{- $type := "series" -}} {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}

 {{ .Page.Title }} {{- len $pages -}}

{{- range first 5 $pages -}} {{- end -}} {{- if gt (len $pages) 5 -}} {{ T "more" }} >> {{- end -}}
{{- end -}} {{- end -}}

分类

{{- $terms := $.Site.Taxonomies.categories.ByCount -}}
{{- range $terms -}} {{- $term := .Term -}} {{- $pages := .Pages -}} {{- $type := "categories" -}} {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}

 {{ .Page.Title }} {{- len $pages -}}

{{- range first 5 $pages -}} {{- end -}} {{- if gt (len $pages) 5 -}} {{ T "more" }} >> {{- end -}}
{{- 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 */ -}}

所有文章   标签   系列   分类   画廊   作品


标签

{{- range $.Site.Taxonomies.tags.ByCount -}} {{ .Page.Title }} {{ .Count }} {{- end -}}

系列

{{- $terms := $.Site.Taxonomies.series.ByCount -}}
{{- range $terms -}} {{- $term := .Term -}} {{- $pages := .Pages -}} {{- $type := "series" -}} {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}

 {{ .Page.Title }} {{- len $pages -}}

{{- range first 5 $pages -}} {{- end -}} {{- if gt (len $pages) 3 -}} {{ T "more" }} >> {{- end -}}
{{- end -}} {{- end -}}

分类

{{- $terms := $.Site.Taxonomies.categories.ByCount -}}
{{- range $terms -}} {{- $term := .Term -}} {{- $pages := .Pages -}} {{- $type := "categories" -}} {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}

 {{ .Page.Title }} {{- len $pages -}}

{{- range first 10 $pages -}} {{- end -}} {{- if gt (len $pages) 10 -}} {{ T "more" }} >> {{- end -}}
{{- 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 -}}

 未分类 {{- len $pages -}}

{{- range $pages -}} {{- 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; } } } ```