Setting params for taxonomy pages

I have set up pages for each category and each tag on my site, as can be seen, for instance, here and here. I’ve also arranged for the category pages (though not the tags) to have URLs immediately below the root, like this:

[permalinks] 
...
categories= "/:slug/"

The idea is to have an introductory text followed by the list of posts included in the category or tagged with the corresponding term. The corresponding code is:

{{ define "main" }}
<article class="archive">
  <a class="button" href="/articles/">Back to Articles</a>
  <div class="content">
    <h1>Category: {{ .Title }}</h1>
    {{ ***** TEXT WOULD BE HERE ***** }}
  </div>
  <div class="grid cards">
    {{ range .Paginator.Pages }}
    <div class="column is-half">
      {{ partial "widgets/post-card.html" . }}</div>
    {{ end }}
  </div>
</article>
{{ end }}

I’ve tried including Markdown pages in folders matching those of the categories (i.e., ‘politics’, ‘tech’, ‘sundry’, etc.) in /content/ in whose front matter I could include a description. But that didn’t work. I could of course include the texts for each category in the Site params in config.toml, but that seems a rather convoluted way of achieving what I want. Any suggestions?

hugo new categories/_index.md (taxonomy page)
hugo new categories/foo/_index.md (term page)

https://gohugo.io/content-management/taxonomies/#add-custom-metadata-to-a-taxonomy-or-term

Thanks very much! (That was very quick.) But how would I reference the parameters for ‘foo’, since I need to do this from within /layout/categories/taxonomy.html, in the place in the code I referred to above?

Here’s a simple example.

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

Files of interest:

  • layouts/categories/taxonomy.html
  • layouts/categories/term.html

That’s a really great tool.

I seem to have got taxonomy and terms reversed. Using this article as a base, I ended up with individual category pages generated by the /layouts/taxonomy.html template—whereas it’s apparent from your repository that I should using terms.html for that, in order to be able to access the Params set for each of the individual terms.

(My terms.html is spewing out a list of categories, and I haven’t got the code for it work yet. My respository is here.)

Is there any way out other than shifting the code from one template to the other? And how was it even possible to get the templates working at cross-purposes in the first place?

That article is outdated. In 2020 we finally corrected the terminology:
https://github.com/gohugoio/hugo/pull/7394

Following my example, your templates should be:

layouts/
├── categories/
│   ├── taxonomy.html --> lists all the terms in a given taxonomy
│   └── term.html     --> lists all the pages with a given term
└── _default/
    ├── baseof.html
    ├── home.html
    ├── list.html
    └── single.html

Note that the term template is term.html, not terms.html.

1 Like

I see. The author of that article stated:

There are two kinds of pages for taxonomies, and we need to implement them both. With taxonomies terms pages show the list of taxonomies, for example the categories page will show all categories of the site. On the other hand taxonomy pages show content that is related to one taxonomy item, such as “Nature” in site categories.

Thanks for setting me back on the right path.

Problem solved thanks to you. Since I already have an /articles.html template that lists all my posts by category, the taxonomy.html template is totally redundant for my needs and I set a 301 redirect from /categories to /articles. Just two additional small points:

1. I used the following code to display the content of each category page’s description parameter in the /content/articles/section.html template:

    <p class="subtitle">{{ with site.GetPage "categories/sundry" }}
    {{ .Params.description | markdownify }}
    {{ end }}</p>

I suppose there’s nothing less verbose to achieve that?

2. Just out of curiosity, I’m assuming I can include exactly the same content in /content/tags and get a list of pages that are tagged with a particular term?

Sure, just do what you did with the categories taxonomy.

1 Like

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