Some of my posts are lists of cases, which change over time. Ideally, I want each post to show when I created the original post and when I updated it.

PaperMod’s layout for post metadata, layouts/partials/post_meta.html, does not include the date the post was last modified. Fortunately, Hugo has a predefined frontmatter variable, lastmod, which allows an author to store that data. Even better, Hugo’s Git integration can derive the lastmod value from the file’s Git history.

Hugo will automatically pull the lastmod value from the Git history, but the author can manually override the lastmod value in the frontmatter. Mert Bakir wrote a great post about integrating the two approaches.

I adapted the solution in PaperMod. Firest, I added this key to config.yaml:

1
enableGitInfo: true

Next, I copied the theme’s layouts/partials/post_meta.html to my project folder and modified it. The code below displays the lastmod date only if it differs from the frontmatter date.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{{ $date := .Date.Format "02.01.2006" }}
{{ $lastmod := .Lastmod.Format "02.01.2006" }}

{{- $scratch := newScratch }}

{{- if not .Date.IsZero -}}
{{- $scratch.Add "meta" (slice (printf "<span title='%s'>%s</span>" (.Date) (.Date | time.Format (default "2006-01-02" site.Params.DateFormat)))) }}
{{- end }}

{{- if ne $lastmod $date -}}
{{- $scratch.Add "meta" (slice (printf "<span title='%s'>(updated %s)</span>" (.Lastmod) (.Lastmod | time.Format (default "January 2, 2006" site.Params.DateFormat)))) }}
{{- end }}

...

{{- with ($scratch.Get "meta") }}
{{- delimit . "&nbsp;·&nbsp;" -}}
{{- end -}}

It works great.