本文以当前博客为例,把 PaperMod 归档页改为“年份 → 月份 → 文章”的二级折叠结构。当前项目使用:
1
2
|
content/archives.md
layouts/_default/archives.html
|
PaperMod 和 Hugo 的模板查找规则会随主题版本及目录结构变化。当前仓库已经通过 layouts/_default/archives.html 成功覆盖主题模板,因此本文沿用这个路径。若你的主题使用根级 layouts/archives.html,应以当前主题同名模板和 Hugo 的模板调试结果为准,不要同时保留两份归档覆盖。
创建归档页面#
创建 content/archives.md:
1
2
3
4
5
6
|
---
title: "归档"
layout: "archives"
url: /archives/
summary: 文章归档页面
---
|
当前 hugo.yaml 已经把归档加入主菜单:
1
2
3
4
5
6
|
menu:
main:
- identifier: archives
name: 归档
url: /archives/
weight: 10
|
覆盖归档模板#
创建或编辑 layouts/_default/archives.html。页面先取得主内容分区中的普通页面:
1
2
3
4
|
{{- $pages := where site.RegularPages "Type" "in" site.Params.mainSections }}
{{- if site.Params.ShowAllPagesInArchive }}
{{- $pages = site.RegularPages }}
{{- end }}
|
然后按发布日期分组。年份、月份分组和文章显示应统一使用 PublishDate,避免文章被分到一个月份却显示另一个日期:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
{{- range $pages.GroupByPublishDate "2006" }}
{{- if ne .Key "0001" }}
{{- $year := .Key }}
<section class="archive-year">
<details>
<summary class="archive-year-header" id="{{ $year }}">
<span class="archive-toggle" aria-hidden="true"></span>
{{ $year }} 年
<sup class="archive-count">{{ len .Pages }} 篇</sup>
</summary>
<div class="archive-year-content">
{{- range .Pages.GroupByPublishDate "01" }}
{{- $month := .Key }}
<details>
<summary class="archive-month-header" id="{{ printf "%s-%s" $year $month }}">
<span class="archive-toggle" aria-hidden="true"></span>
{{ $month }} 月
<sup class="archive-count">{{ len .Pages }} 篇</sup>
</summary>
<div class="archive-posts">
{{- range .Pages.ByPublishDate.Reverse }}
{{- if eq .Kind "page" }}
<article class="archive-entry">
<h3 class="archive-entry-title entry-hint-parent">
<a href="{{ .RelPermalink }}">{{ .Title | markdownify }}</a>
{{- if .Draft }}
<span class="entry-hint" title="Draft">[草稿]</span>
{{- end }}
<span class="archive-date-postfix">{{ .PublishDate.Format "01-02" }}</span>
</h3>
<div class="archive-meta">{{ partial "post_meta.html" . }}</div>
<a class="entry-link" aria-label="post link to {{ .Title }}" href="{{ .RelPermalink }}"></a>
</article>
{{- end }}
{{- end }}
</div>
</details>
{{- end }}
</div>
</details>
</section>
{{- end }}
{{- end }}
|
数字月份布局 "01" 不依赖英文月份名称,适合当前 defaultContentLanguage: zh 和 locale: zh-CN 配置。使用 .RelPermalink 可以兼容域名、语言前缀和子路径部署。
添加归档统计#
在页面标题下显示文章总数:
1
2
3
4
5
6
7
|
<header class="page-header">
<h1>{{ .Title }}</h1>
{{- with .Description }}
<div class="post-description">{{ . | markdownify }}</div>
{{- end }}
<div class="archive-total">共 {{ len $pages }} 篇文章</div>
</header>
|
添加样式#
当前站点把样式写在归档模板内,因此样式只随归档页面输出。若希望模板更易维护,也可以将同样的规则移动到 assets/css/extended/archive.css。
无论采用哪种方式,都应使用归档类名限定选择器,不要直接写全局 details summary 或 summary:hover:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
.archive-year summary {
cursor: pointer;
list-style: none;
user-select: none;
}
.archive-year summary::-webkit-details-marker {
display: none;
}
.archive-toggle::before {
display: inline-block;
width: 0;
height: 0;
border-top: 0.3em solid transparent;
border-bottom: 0.3em solid transparent;
border-left: 0.42em solid currentColor;
content: "";
transition: transform 0.15s ease;
}
.archive-year details[open] > summary .archive-toggle::before {
transform: rotate(90deg);
}
.archive-year-header {
margin: 1.1em 0 0.25em;
font-size: 1.9em;
font-weight: 600;
}
.archive-month-header {
margin: 0.6em 0 0.15em 1.5em;
font-size: 1.55em;
font-weight: 600;
}
.archive-year-header,
.archive-month-header {
display: flex;
flex-wrap: wrap;
align-items: center;
}
.archive-year-content {
margin-top: -0.15em;
margin-left: 1.5em;
}
.archive-posts {
margin: 0.2em 0 0.8em 3em;
padding-left: 0.5em;
}
.archive-count,
.archive-date-postfix,
.archive-total {
color: var(--secondary);
opacity: 0.9;
}
@media (max-width: 600px) {
.archive-year-header {
font-size: 1.55em;
}
.archive-month-header,
.archive-year-content {
margin-left: 0.75em;
}
.archive-posts {
margin-left: 1.25em;
padding-left: 0;
}
}
|
使用 CSS 三角形绘制箭头,不依赖字体中的 >、v 等字形,可以避免当前自定义字体导致箭头宽度或基线不一致。
日期说明#
GroupByPublishDate 使用 Hugo 解析后的 PublishDate。如果文章只有 date,Hugo 通常会按默认日期配置将它作为发布日期;如果项目自定义了 frontmatter.date、publishDate 的候选字段,应检查缺失字段的文章是否仍进入预期年份。
不要把按普通日期分组的旧写法与按发布日期显示的写法混用。旧写法通常类似于对 .Pages 调用 GroupByDate 并传入月份名称,而文章标题旁却使用:
1
|
.PublishDate.Format "01-02"
|
前者可能依据另一日期分组,后者却显示发布日期,最终造成分组和页面日期不一致。
运行:
1
2
|
hugo --cleanDestinationDir --destination public-check
hugo server --disableFastRender
|
检查 /archives/:
- 年、月默认折叠,点击后可正常展开。
- 年份和月份按发布日期倒序排列。
- 同月文章按
PublishDate 倒序排列。
- 锚点
/archives/#2026-10 能定位到对应月份。
- 文章总数与实际归档文章数一致。
- 手机页面没有横向滚动。
- 页面中的其他
details、summary 不受归档样式影响。
修改前备份 layouts/_default/archives.html。出现构建错误时恢复该文件并重新运行 Hugo。若删除站点覆盖模板,PaperMod 会回退到主题自带的归档模板;content/archives.md 和菜单项可以继续保留。
参考资料#