How to list only pages with certain tags?

On my homepage I have it list all of the pages on my website:

<ul class="patterns-list" id="list">
    {{ range .Site.Pages }}
    <li>
      <h2>
        <a href="{{ .Permalink }}">
          <svg
            class="bookmark"
            aria-hidden="false"
            viewBox="0 0 40 50"
            focusable="false"
          >
            <use xlink:href="#bookmark"></use>
          </svg>
          {{ .Title }}
        </a>
      </h2>
    </li>
    {{ end }}
  </ul>

instead of listing ALL of the pages, I’d like to only list the ones with certain tags. For example, let’s say I wanted to list all pages on my site with the tags “red” OR “blue”.

How should I modify the “{{ range .Site.Pages }}” section to accomplish this?

{{- $p1 := where .Site.Pages "Params.tags" "intersect" (slice "red") -}}
{{- $p2 := where .Site.Pages "Params.tags" "intersect" (slice "blue") -}}
{{- range (union $p1 $p2) -}}
  ...
  ...
{{- end -}}

See https://gohugo.io/functions/where/#use-where-with-intersect

3 Likes

Thank you very much!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.