How to Show Most Recently Updated Posts in Hugo
Published: 2024-02-04 | Updated: 2024-05-16When I first started this site I underestimated the number of times I’d go back and update old material.
Old posts need updating because:
- I found a newer and better way of accomplishing something.
- I reconsidered an old decision leading to some old recommendation which I no-longer think is the best option.
- New versions of software happen, making the old ways of doing things less relevant
- It’s the internet. Everything changes all the time
So that’s why my site now leads with the most recently updated material. Hey, I did the work to refresh this thing I wrote 2 years ago. Might as well showcase it, eh?
To that end , this site’s home page now features my 5 most-recently updated 5 posts. Here is the template logic that makes it happen.
<section class="recent-posts">
{{ range (where site.RegularPages "Type" "posts").ByLastmod.Reverse | first 5 }}
<div class="post">
<h2>
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
</h2>
<p class="date">
{{ .Date.Format "2006-01-02" }}
</p>
<div>
{{ .Description }}
</div>
</div>
{{end }}
</section>