HUGO

Listing content with the same taxonomy term

In page, I hope to show all titles of contents which have same taxonomy term.
for example,

A.md

+++
title = "A title"
categories = “C”
+++

B.md

+++
title = "B title"
categories = “C”
+++

in page A, B


related posts : A title, B title

I thought docs deal with this.
So I tried like the following though I don’t get it, yes it didn’t work. (What is .golang?? A series name?)

<ul>
   {{ range .Site.Taxonomies.categories.golang }}
   <li><a href="{{ .URL }}">{{ .Name }}</a></li>
   {{ end }}
</ul>

What should I do?

ps. Hugo docs seems pretty hard to understand for newbies…

1 Like

“golang” is a category – one of the defined taxonomies.

@cheesemelon First of all, try that piece of code using a category or tag you have defined in your site. I had the problem described in issue 608 – which should be re-opened maybe? – and I managed to get it working using .Page.Url and .Page.Title:

<ul>
  {{ range .Site.Taxonomies.tags.CHANGEIT }}
    <li><a href="{{ .Page.URL }}">{{ .Page.Title }}</a></li>
  {{ end }}
</ul>

However, this example requires to define a template for every single tag/category.
I’d like to define a default taxonomy template which lists all the content for each item in the taxonomy. I guess that I’ll have to use two range loops to achieve that, but I’m afraid that I’m not good enough to make it. I’ll make a try and come back in case I do…

1 Like

What I wanted to do was easier than expected. I created taxonomy/tag.html with this content:

{{ partial "header.html" . }}

<section id="main">
  <div class="container">
   <h1 id="title">{{ .Title }}</h1>
    {{ range .Data.Pages }}
        <ul>
            <li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
        </ul>
    {{ end }}
  </div>
</section>

{{ partial "footer.html" . }}

It seems that Hugo is smart enough to list only the items tagged with the specific tag: when I go to tags/wow/ I see only the items tagged as “wow”.

Anyway, I didn’t mean to hijack your thread with different questions.
I realize now that you are trying to do something different.

1 Like
<ul>
  {{ range .Site.Taxonomies.tags.CHANGEIT }}
    <li><a href="{{ .Page.URL }}">{{ .Page.Title }}</a></li>
  {{ end }}
</ul>

Can I make CHANGEIT dynamic.

Use case:
suppose you are visiting a page call a. a page has categories params ['abc']. incase of b page and c page categories params also ['abc']. I mean page a page b & page c all under abc category. So when you visit page a I want to see all 3 pages and make active a page. The solution should be taxonomies centric

if i think about javascript
let obj = {
  city: 'dhaka',
  country: 'bangladesh',
   'hdi': 'low',
}

obj.city // dhaka
obj['city'] // dhaka
let x = 'city'
obj[x] // dhaka 

{{ range .Params.tags }} <!-- iterating tags params  -->
 <li>
   <a href="{{$.Site.BaseURL}}tags/{{ . | urlize }}">
      {{ . }}
    </a>
    <ul>
      {{$tagName := .}} <!-- set tag term -->

      {{ range .Site.Taxonomies.tags.[$tagName] }} <!-- iterating tag term. and how can I make it dynamic-->
        <li><a href="{{ .Page.URL }}">{{ .Page.Title }}</a></li>

      {{end}}
    </ul>
 </li>

{{ end }}

I mean 

{{ range .Site.Taxonomies.tags.CHANGEIT }}

I mean How can I CHANGEIT dynamically

Thanks

2 Likes