-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Description
I'd like to generate a random* list of post in Hugo; the idea is to place links to the first 5 of these below the posts as a way of "read more".
For this I was thinking about something like:
{{ range first 5 (where .Data.Pages.Random "Section" "post") }}
{{ .Title }}
{{ end }}
The idea is that Hugo, instead of generating a list ordered by date (.Data.Pages.ByDate), a list generates with all posts in .Data.Pages ordered in a random manner. Then we can use the existing range, first, and where functions to "pull" the number of random posts from the list as we'd like.
One requirement for this is that Hugo generates a random list of posts with a new seed each time .Data.Pages.Random is come across. Otherwise the list of posts will be random between builds, but not during builds.
*: I know that such a list would be, theoretically speaking, pseudo-random. But I'm more than fine with a "seemingly random" list, especially since that would be easier to implement and is much friendlier on the build times.
(A related forum topic is here: https://discuss.gohugo.io/t/psuedo-random-list-of-category-posts/1923 ).