A “read more” link in GoHugo

Internet

Hugo supports the WordPress-style <!--more--> tag in blog posts, which split your text into a summary and body. What if you wanted your template to always show the full text of a post, unless you provided a manual summary?

Define this in your index.html template:

{{ if .Truncated }}
    {{ .Summary }}
    <a href="{{ .Permalink }}">Read more…</a>
{{ else }}
    {{ .Content }}
{{ end }}

This almost works. Hugo automatically generates summaries for posts that don’t include them, so .Truncated always evaluates true by default.

The trick (hack?) is to define a massive default summary length in your site config, so it will (practically) never autogenerate them:

summaryLength = 999999

Now the template will show a summary for posts that have them, otherwise they’ll default to showing the whole post.

Author bio and support

Me!

Ruben Schade is a technical writer and infrastructure architect in Sydney, Australia who refers to himself in the third person. Hi!

The site is powered by Hugo, FreeBSD, and OpenZFS on OrionVM, everyone’s favourite bespoke cloud infrastructure provider.

If you found this post helpful or entertaining, you can shout me a coffee or send a comment. Thanks ☺️.