Add a Last Modified Date in Hugo Articles
Add a Last Modified Date in Hugo Articles
Add date data to our Hugo posts
- Last Modified
- Last Updated
- Edited
Conditionally render a Last Modified date in the single.html template for our posts.
Published {{ .Date.UTC.Format "Jan 2, 2006" }}
Last Modified {{ .Lastmod.UTC.Format "Jan 2, 2006" }}
Use Hugo’s date and lastmod fields.
Two ways we go about this:
1. Automatically add lastmod field using Git metadata
Automatically add the lastmod field to each article based on Git information.
Update enableGitInfo in config.toml
Set enableGitInfo to true in our config.toml.
enableGitInfo = true
The lastmod field will default to the most recent commit date for that file in Git.
Update frontmatter in config.toml
If enableGitInfo is true, it will override any lastmod dates in front matter.
By default, :git comes before lastmod. To override add the following lines to the config file to config.toml
enableGitInfo = true
[frontmatter]
date = ["date", "publishDate", "lastmod"]
lastmod = ["lastmod", ":git", "date", "publishDate"]
publishDate = ["publishDate", "date"]
expiryDate = ["expiryDate"]
2. Manually add lastmod to front matter
Second approach is to manually add lastmod fields to front matter whenever an article is updated.
We want the Last Modified date to show only if we explicitly set it in the front matter.
Add lastmod to front matter
Add the lastmod field to our article front matter:
date: "2022-01-10"
lastmod: "2022-01-15"
If enableGitInfo is true, lastmod will be set to the most recent commit date for that file and override any lastmod dates.
Cange this behavior by configuring front matter dates: ["lastmod", "date", "publishDate"]
Created a simple partial template
This partial template checks if the lastmod and publish dates are the same then renders the dates. Compare two dates, Jan 2, 2006 is equal to mm.dd.yyyy.
Create lastmod.html
{{ $date := .Date.UTC.Format "Jan 2, 2006" }}
{{ $lastmod := .Lastmod.UTC.Format "Jan 2, 2006" }}
<li class="list-inline-item d-flex align-items-center">
<i class="fas fa-calendar me-2"></i>
<span>Published on {{ $date }}</span>
<li class="list-inline-item d-flex align-items-center">
<i class="fas fa-calendar me-2"></i>
{{ if ne $lastmod $date }}
<span> Edited on {{ $lastmod }}</span>
{{ end }}
</li>
In single.html or post.html this can be called with the following {{ partial "lastmod.html" . }}
Summary
Minor update to improve information.
Check out the something interesting:
Related Posts
2023 Phoenix VMUG UserCon
Introduction: The recent 2023 Phoenix VMUG UserCon brought together some like-minded people in the field, with discussions ranging from VMware technologies to best practices for optimizing existing systems.
Read moreRed Hat User Group Insights, Ansible Automation Platform, and ITSM Integration
Introduction: This blog post aims to summarize the key takeaways from this informative workshop. At the recent Red Hat User Group workshop on Red Hat Insights, Red Hat Ansible Automation Platform, and their integration with management (ITSM) systems, such as ServiceNow, provided valuable insights into how these technologies work together.
Read moreRobocopy Examples
Robocopy Examples Robocopy has many command line options and it can be overwhelming to know which commands to use. In this post, we will take a look at how to ues robocopy to copy, mirror, purge Files and Folders.
Read more