With frequently changing information it can be useful to record both when a post was created and when it was last updated - showing that it’s been reviewed recently. We do this on this site by showing the created date and showing the last modified date if it’s different from the created date (see above the title).
There’s two ways of doing this in Hugo:
recording when you last changed it
using the last changed date in Git (if you use it)
(or a blend of the two)
The Manual Way:
The simplest way is to record a lastmod date and time within your post’s header - like the example below:
1
2
3
4
5
6
---
title: My Example Post
date: 1990-01-01T00:00:00+00:00
lastmod: 1995-04-04T00:00:00+00:00
url: /example-post/
---
We can then use this information in our theme and layouts to display the information. In our code, we don’t show the last updated date if it’s the same as the created date (there’s no point?)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- Created Date -->{{- $pubdate := .PublishDate.Format "02.01.2006" }}
Created:
<timedatetime="{{ .PublishDate }}"title="{{ .PublishDate }}"> {{ $pubdate }}
</time><!-- Last Updated Date -->{{- if .Lastmod }}
{{- $lastmod := .Lastmod.Format "02.01.2006" }}
{{- if ne $lastmod $pubdate }}
<divclass="post-info-last-mod"> (Updated:
<timedatetime="{{ .Lastmod }}"title="{{ .Lastmod }}"> {{ $lastmod }}
</time>)
</div> {{- end }}
{{- end }}
Output:
1
2
Created: 01.01.1990
(Updated: 04.04.1995)
You can of course change the formatting to be however you would like. We do have a post on showing it with ordinals like ’th’ and ‘st’ if it’s useful.
The Git way
Hugo can actually hook into your git (the version control system) information and pull the last edited times from there. To enable it, just change this setting in your config.
config.toml
1
enableGitInfo=true
This will now automatically pull in the last updated times and fill in lastmod for you - Neat!
It will, however, overwrite any of your manually created lastmod dates. You can choose to change this behaviour to favour your lastmod times but if they don’t exist then use git. To do this, we need to add one final part to our config changing the front matter dates:
Edd is a PHP and Go developer who enjoys blogging about his experiences, mostly about creating and coding new things he's working on and is a big beliver in open-source and Linux.
Add a 404 Not Found Page
–
In an ideal world, every web page would exist, but sometimes links go to pages that doen’t exist. This is why we make “404 - not found” pages. In Hugo you can do the same. Many of our sites are hosted with Netlify which also support these files - automatically returning the correct http status code for them.
To make yours, you can add a file into your layouts folder.
Using Emoji In Posts & Themes
–
Emojis have been named word of the year and are seen every day in modern culture. We use them every day on our messaging apps, emails and … err blogs!
This post is about how you can enable and use them in both your Hugo themes and, in general, when writing posts.
Using Emojis in Themes To use them within your theme, there’s a built in function which turns text in emojis.
Hide a Page in Hugo
–
Option 1: Draft The simplest way to hide a page when using Hugo is probably to set the post as a draft. This way, you still have the content ready to go for when you need it.
To hide a page by setting it as draft, so our drafts page:
Set Post as Draft Tutorial Option 2: Hide from Posts List You still want the page to be visible, but you don’t want to show it your blog posts list?