How can I get 3 random elements and map them except one specific?

For more context
Right now I’m building a blog and I need to show 3 random posts in the end of each post. But! In these random posts should be a post that is currently open.

For example, I have such posts:
Post 1
Post 2
Post 3
Post 4
Post 5
Post 6

And I opened “Post 4”, at the end of it I should see 3 random posts but “Post 4” can’t be there. That’s what I’m trying to accomplish. Unfortunately, I don’t really know Go so it’s kinda hard for me but I know how to map elements :slight_smile:

1 Like

Try something like this (assuming you add it to your single.html template, or some variant)

{{ $random_pages := site.RegularPages | complement (slice .) | shuffle | first 3 }}

{{ range $random_pages }}
<!-- rest of your code... -->

What it does:

  • looks through all regular pages that are not the current page
  • shuffles 'em (AKA makes 'em random)
  • returns the first 3
4 Likes

It works, thank you so much!

1 Like