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

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>&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

{{- 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 -}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值