Random post in Hugo
I wanted a way to link to a random post of the same content type. The solution wasn't readily available, I googled around and came up with this:
{{ range first 1 (shuffle (where (where (where .Site.Pages "Type" "post") "Title" "!=" "posts") "Title" "!=" .Title)) }}
<div>
<a href="{{ .URL }}">Go to another random post</a>
</div>
{{ end }}
Kinda verbose isn't it? The 3 where clauses are to:
- Filter by current post title
- Filter by content type, it has to be post
- Filter out the list page for the content type
But it works, ish. The downside is, since Hugo is a static site generator, the random links are only generated once.