Discussionsを見て#
PagerModのDiscussionsを見たら、リスト表示(list.html)にタグを入れたいという投稿がありました。
シングル記事(single.html)の本文下には元から入っているんですが、それをリストにも出したいと。
🔗 How to display tags in the post list? · adityatelange/hugo-PaperMod · Discussion #606
投稿自体は2021年と古かったのですが、最近になって解決したようでトップに上がっていました。
完成形#
リスト表示

シングル表示

シングル表示のほうはそれぞれのタグにリンクが付いていて、同タグ一覧を開くことが可能です。
やり方#
スレッドに書いてあるとおりなんですけど、
1
2
3
4
5
6
7
8
| {{- $tags := .Params.tags -}}
{{- if $tags -}}
{{- $lastIndex := sub (len $tags) 1 -}}
{{- range $index, $tag := $tags -}}
<a href="/blog2/tags/{{ $tag | urlize }}"> {{ $tag }}</a>
{{- if ne $index $lastIndex }} · {{ end -}}
{{- end -}}
{{- end -}}
|
ファイル名はなんでもオッケー。
urlize でURLを作成します。
/blog2/ はうちのサイト用に入れています。
表示するための部品(post_meta_cattag.html)を用意する#
1
2
3
4
5
6
7
8
9
| {{- $scratch := newScratch }}
{{- with (partial "tags.html" .) }}
{{- $scratch.Add "meta" (slice " / [" . "]")}}
{{- end}}
{{- with ($scratch.Get "meta") }}
{{- delimit . " " | safeHTML -}}
{{- end -}}
|
こちらのファイル名もなんでもオッケー。
中ではさきほど作成したファイル名 tags.html を利用しています。
リスト表示(list.html)の画面に表示する#
1
2
3
4
5
6
| {{- if not (.Param "hideMeta") }}
<footer class="entry-footer">
{{- partial "post_meta.html" . -}}
{{- partial "post_meta_cattag.html" . -}}
</footer>
{{- end }}
|
一部分だけ抜き出しましたが、{{- partial "post_meta_cattag.html" . -}} を追加します。
おまけでシングル表示(single.html)の上部にも表示する#
1
2
3
4
5
6
7
| <div class="post-meta">
{{- partial "post_meta.html" . -}}
{{- partial "post_meta_cattag.html" . -}}
{{- partial "translation_list.html" . -}}
{{- partial "edit_post.html" . -}}
{{- partial "post_canonical.html" . -}}
</div>
|
こちらも一部分だけ抜き出し。
{{- partial "post_meta_cattag.html" . -}} を追加します。