How to Show Most Recently Updated Posts in Hugo

Published: 2024-02-04 | Updated: 2024-05-16

When I first started this site I underestimated the number of times I’d go back and update old material.

Old posts need updating because:

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>