# Template logic for Related posts

**URL:** https://discourse.gohugo.io/t/template-logic-for-related-posts/2947
**Category:** support
**Created:** [March 19, 2016, 11:11pm UTC](https://discourse.gohugo.io/t/template-logic-for-related-posts/2947 "2016-03-19T23:11:30Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Ravi\_Kumar](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@Ravi\_Kumar](https://discourse.gohugo.io/u/Ravi_Kumar)
#### Post date: [March 19, 2016, 11:11pm UTC](https://discourse.gohugo.io/t/template-logic-for-related-posts/2947/1 "2016-03-19T23:11:30Z")

</div>

Taking code from [http://justindunham.net/blog-bells-and-whistles-in-hugo/](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: -

```auto
    <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.

---

<div class="post-metadata">

### Author: ![anon34662560](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@anon34662560](https://discourse.gohugo.io/u/anon34662560)
#### Post date: [February 12, 2017, 11:38am UTC](https://discourse.gohugo.io/t/template-logic-for-related-posts/2947/3 "2017-02-12T11:38:37Z")

</div>

I found a more performant code snippet [dropped last August](https://novelist.xyz/tech/related-posts-in-hugo/). I’ve adapted it in [After Dark](https://github.com/comfusion/after-dark) to support conditional list display. Here’s the end result:

```
{{ range first 1 (where (where .Site.Pages ".Params.tags" "intersect" .Params.tags) "Permalink" "!=" .Permalink) }}
  {{ $.Scratch.Set "has_related" true }}
{{ end }}

{{ if $.Scratch.Get "has_related" }}
  <aside>
    <header>Related Content</header>
    <ul>
      {{ $num_to_show := .Site.Params.related_content_limit | default 7 }}
      {{ range first $num_to_show (where (where .Site.Pages ".Params.tags" "intersect" .Params.tags) "Permalink" "!=" .Permalink) }}
        <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
      {{ end }}
    </ul>
  </aside>
{{ end }}

```

This uses uses `range` to loop over pages until it finds its first match, setting a Boolean in `$.Scratch`, which is then used to conditionally output 7 related pages, with optional limit override, in reverse chronological order (the Hugo default). Note this will grab pages across sections and not just posts.

---

<div class="post-metadata">

### Author: ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)
#### Post date: [August 29, 2019, 7:24pm UTC](https://discourse.gohugo.io/t/template-logic-for-related-posts/2947/4 "2019-08-29T19:24:15Z")

</div>


