HUGO

How to put in index all pages with tags

I have a hugo site, that contains a lot of md files from submodules. And not all of them should go to the index. I want to create an index with all pages, which have any tags. I tried to create a ghost tag, which would be put on every index page, but it doesn`t seems to be a good solution.

{{ range site.Taxonomies.tags }}
  {{ range .Pages }}
    <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
  {{ end }}
{{ end }}

Yes, it found all pages with tags. But if a page has more than one tag, it presents in index.json corresponding times

Sorry about that. It’s morning here in Seattle, and I have only had one cup of coffee. A poor excuse, but an excuse nevertheless.

Option 1

{{ $p := slice }}
{{ range site.Taxonomies.tags }}
  {{ range .Pages }}
    {{ $p = $p | append . }}
  {{ end }}
{{ end }}

{{ range uniq $p }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}

Option 2

{{ range where site.Pages "Params.tags" "ne" nil  }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}

The only limitation with the second approach is that it will include pages with an empty tag array:

tags = []
1 Like

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