Yearly archive for Hugo

Source: Super simple yearly post archives for Hugo

1. Install shortcode

Save the following snippet as layouts/shortcodes/archive.html.

Note that you might have to change “posts” in the code below to whatever Hugo section contains your blog posts.

{{/*

simple shortcode to display all posts in yearly sections on your archive page
by Charl P. Botha

this is a much simplified version of the yearly post archiving template at:
https://rohanverma.net/blog/2019/11/15/archive-pages-group-by-year-hugo/

*/}}

{{ $prev := 3000}}
{{range where .Site.RegularPages "Section" "posts"}}
{{if .Date}}
{{if gt $prev (.Date.Format "2006")}}
## {{ .Date.Format "2006" }}
{{end}}
{{.Date.Format "02 Jan"}} -- [{{.Title}}]({{.Permalink}})
{{ $prev = .Date.Format "2006"}}
{{end}}
{{end}}

2. Use shortcode

Create posts.md in /content/archive/ and add the shortcode

---
title: "Posts Archive"
slug: "posts"
date: 2021-01-09T16:17:00+02:00
type: "page"
---

{ {% archive %} }

Remove the spaces between the {{ }}