Hugo themes Doit 合并 tags , categories 为检索页
总觉得 tags, categories 等页面可以合并成为一页。这样检索起来更方便一些。成果 https://www.ftls.xyz/retrieval/
初始配置
config.toml
# 用于分类的设置
[taxonomies]
author = "authors"
category = "categories"
tag = "tags"
series = "series"
retrieval = "retrieval"
失败尝试
尝试使用 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 前新增的内容
{{- /* Sum Retrieval Page */ -}}
{{- else if eq $taxonomies "retrieval" -}}
<h3><a href="/tags/">>标签</a> <a href="/series/">>系列</a> <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> {{ .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> {{ .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
{{- 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> {{ .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> {{ .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> {{ .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> <a href="/series/">>系列</a> <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> {{ .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> {{ .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 -}}