Displaying lastmod on page only if lastmod is set in frontmatter

I’m a new Hugo user with little knowledge of it. I’ve been playing around with it and some themes for the last few days. I’ve more or less settled on a theme (I think someone did a really good job), but I’d like to slightly tweak it here and there, and I’m trying to do that by looking at other themes that have the configurations I want. However, I’m currently stuck.

TL;DR:

My theme already sets

    <meta property="article:published_time" content="2017-09-13T08:38:00+02:00">
    <meta property="article:modified_time" content="2023-09-13T08:38:00+02:00">

and

  <meta itemprop="datePublished" content="2017-09-13T08:38:00+02:00">
  <meta itemprop="dateModified" content="2023-09-13T08:38:00+02:00">

very nicely out of the box. If lastmod isn’t defined in the frontmatter, it uses date for the meta tags as it should. Perfect!

However, the page itself currently only outputs date, but not lastmod. And I would like it to display something like “Posted on 9 Sep 2017, last updated 9 Sep 2023”. I’m able to to that with

  <time datetime='{{ .Date.Format "2006-01-02" }}' pubdate>
    {{ .Date.Format (default "2006-01-02" .Site.Params.dateFormat) }}
  </time>
      Last updated 
      {{ .Lastmod.Format (default "2006-01-02 15:04:05" .Site.Params.dateFormat) }}

Most of my blog posts don’t contain lastmod, though. The question is, how do I make the output of

      Last updated 
      {{ .Lastmod.Format (default "2006-01-02 15:04:05" .Site.Params.dateFormat) }}

dependent on whether lastmod is set in the frontmatter?

Any help or pointers would be greatly appreciated.

Hugo got buid in, independently, nice solution to displaying nicely the date on posts and you don’t need to do too much.

{{ .Lastmod }}

If your post only got date: in frontmatter, the .Lastmod will display that date. If your post, along the date: got also set lastmod:, the .Lastmod will display that date instead.

On one website I am using such formula

<time datetime='{{ .Date.Format "2006-01-02T15:04" }}'>
   {{ .Date.Format "2 January 2006" }}
</time>
{{ if not (.Date.Equal .Lastmod)  }}
<time datetime='{{ .Lastmod.Format "2006-01-02T15:04:05.000" }}'>update {{ .Lastmod.Format "2 January 2006" }}</time>
{{ end }}

This will display Date only, if is only publish date set as date: and if it got also lastmod: than it will display date with text, updated.

If you just want to display Publish date, and if post got last mod date, only the last mode date, just use .Lastmod approach at the beginning.

3 Likes

Thank you idarek. That’s it!

I’ve changed it slightly to this:

 <time datetime='{{ .Date.Format "2006-01-02" }}' pubdate>
    {{ .Date.Format (default "2006-01-02" .Site.Params.dateFormat) }}
  </time>
{{ if not (.Date.Equal .Lastmod)  }}
  <time datetime='{{ .Lastmod.Format "2006-01-02T15:04:05.000" }}'>, last update 
    {{ .Lastmod.Format (default "2006-01-02" .Site.Params.dateFormat) }}</time>
{{ end }}
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.