Issues with grouping posts by a custom taxonomy

I’ve been working on this for the better part of today and am still rather lost. I’ve found some posts that I think pointed me in the right direction but didn’t actually solve the problem I’m facing. I’m creating a blog that will contain a variety of content and one subject is some Dungeons & Dragons campaigns. I’ve created a custom taxonomy called dnd that stores the name of the campaign for displaying on the page and I want to group posts by that taxonomy. I’ve gotten it somewhat working but the name doesn’t display well. In my frontmatter, I have this:

dnd:
  - Penance & Redemption

or

dnd:
  - New one

^ this is just placeholder text for a new campaign I started yesterday

Those titles, however, are displayed on the page as shown below.

new-one and penance-redemption is all I’m concerned with; why is the “normal” value of dnd (New one and Penance & Redemption) not displayed properly?

{{ if eq $title "Dungeons & Dragons" }}
{{ range $key, $taxonomy := .Site.Taxonomies.dnd }}
<div class="posts-group">
    <div class="post-year">{{ $key }}</div>
    <ul class="posts-list">
        {{- range $taxonomy.Pages }}
        <li class="post-item">
            <a href="{{.Permalink}}">
                <span class="post-title">{{.Title}}</span>
                <span class="post-day">{{ if .Site.Params.dateformShort }}{{ .Date.Format .Site.Params.dateformShort }}{{ else }}{{ .Date.Format "Jan 2"}}{{ end }}</span>
            </a>
        </li>
        {{- end }}
    </ul>
</div>
{{ end }}
{{ end }}

Hi there,

Try this example in the docs: https://gohugo.io/templates/taxonomy-templates/#sitegetpage-for-taxonomies

1 Like

Thank you so much!

For others wanting to accomplish something similar, here’s my code now:

{{ $taxo := "dnd" }}
{{ with ($.Site.GetPage (printf "/%s" $taxo)) }}
{{ range .Pages }}
<h2>{{ .Title }}</h2>
<div class="posts-group">
    <div class="post-year">{{ .Date.Format "2006" }}</div>
    <ul class="posts-list">
        {{ range .Pages.ByParam "dnd" }}
        <li class="post-item">
            <a href="{{.Permalink}}">
                <span class="post-title">{{.Title}}</span>
                <span class="post-day">{{ if .Site.Params.dateformShort }}{{ .Date.Format .Site.Params.dateformShort }}{{ else }}{{ .Date.Format "Jan 2"}}{{ end }}</span>
            </a>
        </li>
        {{ end }}
    </ul>
</div>
{{ end }}
{{ end }}

And the final result:

1 Like

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