I’m trying to create a list of related content. After checking the documentation, I did manage to get a complete list of all the posts that share at least one category with the post the user is currently viewing.
{{ $page_link := .Permalink }}
{{ $categories := .Params.categories }}
{{ range $page := .Site.Pages }}
{{ $has_common_categories := intersect $categories .Params.categories | len | lt 0 }}
{{ if and $has_common_categories (ne $page_link $page.Permalink) }}
<li><a href="{{ $page.Permalink }}">{{ $page.Title }}</a></li>
{{ end }}
{{ end }}
Now what I need to achieve is limit that to just the first 5 items of the list; adding a “first 5” in the range won’t work since it will limit the matching categories for just the first five posts…