Taking code from http://justindunham.net/blog-bells-and-whistles-in-hugo/
How can I first build the slice of related posts, and then decide if length greater than 0, then show “Related articles” block, and render first 5 in the list.
Code: -
<div id="related-posts" class="row">
{{ $page_link := .Permalink }}
{{ $tags := .Params.tags }}
<h3>RELATED ARTICLES</h3>
<ul>
{{ range (where .Site.Pages "Section" "article") }}
{{ $page := . }}
{{ $has_common_tags := intersect $tags .Params.tags | len | lt 1 }}
{{ if and $has_common_tags (ne $page_link $page.Permalink) }}
<li><a href="{{ $page.Permalink }}">{{ $page.Title }}</a></li>
{{ end }}
{{ end }}
</ul>
</div>
I tried {{ range first 5 (where ...)}}
, but this will filter out first 5 post which may not contain the related post.
Is there any way to initialize a slice/array, add related to it and then apply range first N
over this new array/slice.