fix: wrong post count in categories

This commit is contained in:
geekifan
2025-05-03 13:12:55 +08:00
parent 50d5904e96
commit eafc6e8f17
2 changed files with 98 additions and 98 deletions

View File

@ -4,6 +4,13 @@
{{ if or (eq .RelPermalink "/") (eq .RelPermalink (relLangURL "/")) }} {{ if or (eq .RelPermalink "/") (eq .RelPermalink (relLangURL "/")) }}
{{ $is_home = true }} {{ $is_home = true }}
{{ end }} {{ end }}
{{ $is_categories := and (eq .Kind "taxonomy") (eq .Type "categories") }}
{{ $is_category := and (eq .Kind "term") (eq .Type "categories") }}
{{ $is_tags := and (eq .Kind "taxonomy") (eq .Type "tags") }}
{{ $is_tag := and (eq .Kind "term") (eq .Type "tags") }}
{{ $is_post := eq .Type "post"}}
{{ $is_page := eq .Layout "page"}}
{{ $is_archives := eq .Layout "archives"}}
{{- $jsOpts := dict {{- $jsOpts := dict
"target" "es2015" "target" "es2015"
"minify" (eq hugo.Environment "production") "minify" (eq hugo.Environment "production")
@ -70,13 +77,13 @@
{{ $js := "commons" }} {{ $js := "commons" }}
{{ if $is_home }} {{ if $is_home }}
{{ $js = "home" }} {{ $js = "home" }}
{{ else if eq .Layout "categories" }} {{ else if $is_categories }}
{{ $js = "categories" }} {{ $js = "categories" }}
{{ else if eq .Type "post" }} {{ else if $is_post }}
{{ $js = "post" }} {{ $js = "post" }}
{{ else if eq .Layout "page" }} {{ else if $is_page }}
{{ $js = "page" }} {{ $js = "page" }}
{{ else if or (eq .Layout "archives") (eq .Type "categories") (eq .Type "tags") }} {{ else if or $is_archives $is_category $is_tag }}
{{ $js = "misc" }} {{ $js = "misc" }}
{{ end }} {{ end }}

View File

@ -1,114 +1,107 @@
{{ define "main" }} {{ define "main" }}
<article class="px-1"> <article class="px-1">
<h1 class="dynamic-title"> <h1 class="dynamic-title">
{{ i18n "tabs.categories" }} {{ i18n "tabs.categories" }}
</h1> </h1>
<div class="content"> <div class="content">
<div class="container"> {{ $allPages := where .Site.RegularPages "Type" "post" }}
{{ $categories := .Site.Taxonomies.categories }}
<!-- 首先收集所有唯一的父分类 --> <!-- get all parent categories -->
{{ $parentCategories := slice }} {{ $parentCategories := slice }}
{{ range $name, $taxonomy := $categories }} {{ range $allPages }}
{{ range $taxonomy.Pages }} {{ if gt (len .Params.categories) 0 }}
{{ if gt (len .Params.categories) 0 }} {{ $parentCategories = $parentCategories | append (index .Params.categories 0) }}
{{ $parentCategories = $parentCategories | append (index .Params.categories 0) }} {{ end }}
{{ end }}
{{ $parentCategories = $parentCategories | uniq | sort }}
<!-- create card for each parent category -->
{{ range $index, $category := $parentCategories }}
{{ $parentName := . }}
{{ $subcategories := slice }}
{{ $parentPages := slice }}
<!-- get all pages and subcategories related to the parent category -->
{{ range $allPages }}
{{ if gt (len .Params.categories) 0 }}
{{ if eq (index .Params.categories 0) $parentName }}
{{ $parentPages = $parentPages | append . }}
{{ if gt (len .Params.categories) 1 }}
{{ $subcategories = $subcategories | append (index .Params.categories 1) }}
{{ end }}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ $parentCategories = $parentCategories | uniq | sort }} {{ $subcategories = $subcategories | uniq | sort }}
<!-- 然后为每个父分类创建卡片 --> <div class="card categories mb-3">
{{ range $parentCategories }} <div id="h_{{ $index }}" class="card-header d-flex justify-content-between hide-border-bottom">
{{ $parentName := . }} <span class="ms-2">
{{ $subcategories := slice }} <i class="far fa-folder{{ if gt (len $subcategories) 0 }}-open{{ end }} fa-fw"></i>
{{ $parentPages := slice }} <a href="{{ "/categories/" | relLangURL }}{{ $parentName | urlize }}" class="mx-2">{{ $parentName }}</a>
<span class="text-muted small font-weight-light">
<!-- 收集与此父分类相关的所有页面和子分类 --> {{ if gt (len $subcategories) 0 }}
{{ range $name, $taxonomy := $categories }} {{ len $subcategories }}
{{ range $taxonomy.Pages }} {{ if gt (len $subcategories) 1 }}
{{ if gt (len .Params.categories) 0 }} {{ i18n "categories.category_measure" | default (i18n "categories.category_measure.plural") }},
{{ if eq (index .Params.categories 0) $parentName }} {{ else }}
{{ $parentPages = $parentPages | append . }} {{ i18n "categories.category_measure" | default (i18n "categories.category_measure.singular") }},
{{ if gt (len .Params.categories) 1 }}
{{ $subcategories = $subcategories | append (index .Params.categories 1) }}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ end }} {{ len $parentPages }}
{{ end }} {{ if gt (len $parentPages) 1 }}
{{ end }} {{ i18n "categories.post_measure" | default (i18n "categories.post_measure.plural") }}
{{ $subcategories = $subcategories | uniq | sort }} {{ else }}
{{ i18n "categories.post_measure" | default (i18n "categories.post_measure.singular") }}
<div class="card categories mb-3"> {{ end }}
<div class="card-header d-flex justify-content-between hide-border-bottom">
<span class="ms-2">
<i class="far fa-folder{{ if gt (len $subcategories) 0 }}-open{{ end }} fa-fw"></i>
<a href="{{ "/categories/" | relLangURL }}{{ $parentName | urlize }}" class="mx-2">{{ $parentName }}</a>
<span class="text-muted small font-weight-light">
{{ if gt (len $subcategories) 0 }}
{{ len $subcategories }}
{{ if gt (len $subcategories) 1 }}
subcategories,
{{ else }}
subcategory,
{{ end }}
{{ end }}
{{ len $parentPages }}
{{ if gt (len $parentPages) 1 }}
posts
{{ else }}
post
{{ end }}
</span>
</span> </span>
</span>
{{ if gt (len $subcategories) 0 }}
<a href="#sub-{{ $parentName | anchorize }}" data-bs-toggle="collapse" aria-expanded="true">
<i class="fas fa-fw fa-angle-down"></i>
</a>
{{ else }}
<span class="disabled">
<i class="fas fa-fw fa-angle-right"></i>
</span>
{{ end }}
</div>
{{ if gt (len $subcategories) 0 }} {{ if gt (len $subcategories) 0 }}
<div id="sub-{{ $parentName | anchorize }}" class="collapse show"> <a href="#l_{{ $index }}" data-bs-toggle="collapse" aria-expanded="false" aria-label="{{ $parentName }}-trigger" class="category-trigger hide-border-bottom">
<ul class="list-group list-group-flush"> <i class="fas fa-fw fa-angle-down"></i>
{{ range $subcategories }} </a>
{{ $subCategoryName := . }} {{ else }}
<span class="disabled">
<!-- 查找子分类的文章 --> <i class="fas fa-fw fa-angle-right"></i>
{{ $subPages := slice }} </span>
{{ range $parentPages }}
{{ if gt (len .Params.categories) 1 }}
{{ if eq (index .Params.categories 1) $subCategoryName }}
{{ $subPages = $subPages | append . }}
{{ end }}
{{ end }}
{{ end }}
<li class="list-group-item">
<i class="far fa-folder fa-fw"></i>
<a href="{{ "/categories/" | relLangURL }}{{ $subCategoryName | urlize }}" class="mx-2">{{ $subCategoryName }}</a>
<span class="text-muted small font-weight-light">
{{ len $subPages }}
{{ if gt (len $subPages) 1 }}
posts
{{ else }}
post
{{ end }}
</span>
</li>
{{ end }}
</ul>
</div>
{{ end }} {{ end }}
</div> </div>
{{ end }}
</div> {{ if gt (len $subcategories) 0 }}
<div id="l_{{ $index }}" class="collapse show">
<ul class="list-group">
{{ range $subcategories }}
{{ $subCategoryName := . }}
<!-- get pages related to the subcategories -->
{{ $subPages := slice }}
{{ range $parentPages }}
{{ if gt (len .Params.categories) 1 }}
{{ if eq (index .Params.categories 1) $subCategoryName }}
{{ $subPages = $subPages | append . }}
{{ end }}
{{ end }}
{{ end }}
<li class="list-group-item">
<i class="far fa-folder fa-fw"></i>
<a href="{{ "/categories/" | relLangURL }}{{ $subCategoryName | urlize }}" class="mx-2">{{ $subCategoryName }}</a>
<span class="text-muted small font-weight-light">
{{ len $subPages }}
{{ if gt (len $subPages) 1 }}
{{ i18n "categories.post_measure" | default (i18n "categories.post_measure.plural") }}
{{ else }}
{{ i18n "categories.post_measure" | default (i18n "categories.post_measure.singular") }}
{{ end }}
</span>
</li>
{{ end }}
</ul>
</div>
{{ end }}
</div>
{{ end }}
</div> </div>
</article> </article>
{{ end }} {{ end }}