How to get the current tag in a tag listing page?

Hi,

so I have lots of posts, each has one or more tags. I also have a listing for each tag, e.g. /tags/unix for the tag “unix”. I don’t know how to get the current tag in this list in the template.

This is how my template themes/$theme/layouts/_default/taxonomy.html currently looks:

{{ define "main" }}

{{/* this works but I'm sure I'm not using it as intended */}}
{{ $tag := lower .Title}}
<h1>Posts on: {{ $tag }}</h1>

<content>
  {{ $pages := where site.RegularPages "Params.tags" "intersect" (slice $tag) }}
  {{ $paginator := .Paginate ($pages.GroupByDate "January 2006" "desc") }}
  
  {{ range $paginator.PageGroups }}
  {{ range .Pages }}
    <h3><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h3>
    <div>
      {{ .Content }}
    </div>
    <p style="font-size: 0.6em; margin-bottom: 100px">
      <i><a title="permalink" href="{{ .RelPermalink }}">↷ {{ time.Format "02.01.2006" .Date }}</a></i>

      🠶
      {{ range (.GetTerms "tags") }}
      <a class="blog-tags" href="{{ .RelPermalink }}">#{{ lower .LinkTitle }}</a>
    {{ end }}

      <a href="#top">⤒</a>
    </p>
    {{ end }}
    {{ end }}
    
    {{ template "_internal/pagination.html" . }}

</content>
{{ end }}

This works as I want, that is, on the url /tags/unix/ it only lists pages which have the tag “unix”. However, I extract the current tag from the .Title variable. I am pretty sure this is not the correct way to do it.

How would I do this correctly?

I would expect your layout directory to look something like this:

layouts/
└── _default/
    ├── baseof.html
    ├── home.html
    ├── list.html
    ├── single.html
    ├── taxonomy.html  <-- renders the page "tags"
    └── term.html      <-- renders the page "tags/foo"
layouts/_default/taxonomy.html
{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ .Content }}
  {{ range .Pages }}
    <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
  {{ end }}
{{ end }}
layouts/_default/term.html
{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ .Content }}
  {{ range .Pages }}
    <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
  {{ end }}
{{ end }}

So, there’s no need to manually build a page collection. Just range over .Pages.

Additionally, I notice that you are converting the term page’s linkTitle to lowercase. I suspect you want to do this in your site configuration instead:

capitalizeListTitles = false
2 Likes

When I do this, the tag list shows all posts. That was the initial reason why I am doing it the way outlined above.

I must be missing something, because this works great.

git clone --single-branch -b hugo-forum-topic-54364 https://github.com/jmooring/hugo-testing hugo-forum-topic-54364
cd hugo-forum-topic-54364
hugo server

You’re right. I had to use .Pages instead of site.RegularPages!

Thank you very much for your help :slight_smile: :+1:

1 Like

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