Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Generate a random list of posts [Feature request] #1942
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:
The idea is that Hugo, instead of generating a list ordered by date (
One requirement for this is that Hugo generates a random list of posts with a new seed each time
*: 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 ).
It might be a bit easier to understand for a user if we added a random template function that randomly selects elements from an array, map, or slice. To me, your recommendation makes me think they are randomly sorted, which I am not sure is a common use case or efficient processing wise.
For instance, with a random function, we could have the following syntax and it will return a slice with a max of 5 items randomly selected from the input list.
The only con I see to this, is that it could appear as if your data on your page is changing regularly, so would need to keep that in mind w.r.t. SEO and how you use HTML5 elements.
EDIT: Looks like on the master branch is a template function called
This should satisfy you needs.