HUGO

Taxonomies per section

In my site, I have a few different sections to organise content and to make it easier for readers to find articles of interest. To further categorise content, I have leveraged the default Taxonomies. I have not made any changes to my config.toml file for this, using the defaults.

I am now trying to get a list of Taxonomies per Section (tags and categories). I have found that I am unable to query taxonomies by section, for example .Section.Taxonomies or .Site.Section.Taxonomies. It seems the scope of taxonomies is site wide - am I correct in this assumption?

If the scope of Taxonomies is site wide (they are not related to section in any way), does this mean the only way I can get Taxonomies for a section is to query for all Taxonomies and then query for all Taxonomies that have a specific section name?

Question 1 - If I am correct in all of the above, how would I go about efficiently getting Taxonomies per Section, for example all categories within the blog section?

Question 2 - if Hugo considers Taxonomies to be site wide (not related to Section), should I be organising content (grouping) using a different method within Sections?

2 Likes

That depends on what you want to do with your content. I find most folks use taxonomies to no real effect: it doesn’t help users find what they are looking for, as shown by server stats. If you want to share the content and taxonomies you chose, folks can weigh in on your information architecture. :slight_smile:

Sure.

I am not doing too much that is complex. I have three sections for content:

  1. Services
  2. Blog
  3. Portfolio

Blog and Portfolio requires some kind of grouping for the content that allows me to give users the option of viewing articles within a category. This is why I have used categories but I want to make sure they are queried separately so I can put a panel for Blog, Services and Portfolio on the front page.

Is this enough information?

I’ve found these questions on the Hugo discourse channel in regards to filtering Categories by Section.

The following post does not seem to offer a solution

The following post does offer a solution but it seems quite convoluted.

This is another way of having taxonomies per section, though it is a bit of a hack

1 Like

You could use Nested sections.

However, in case you need to use multiple values (or you don’t want to use subfolders for articles), then you can try using taxonomies with slashes that match the section names and use section specific layouts.

config.toml

[taxonomies]
  blog_categories = "blog/categories"
  portfolio_categories = "portfolio/categories"

content/blog/post-1.md

---
"blog/categories": [category-1, category-2]
title: Blog post title
---
Blog post content

Do the corresponding thing with pages under “portfolio”

layouts/blog/single.html

<h2>{{ .Title }}</h2>
<main>{{ .Content }}</main>
<aside>
  <h3>Categories</h3>
  <!-- this is straight from hugo docs -->
  {{ $taxo := "blog/categories" }}
  <ul>
    {{ range .Param $taxo }}
      {{ with site.GetPage (printf "/%s/%s" $taxo (. | urlize)) }}
          <li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
      {{ end }}
    {{ end }}
  </ul>
</aside>

Do the corresponding thing with layouts/portfolio/single.html

This yields independent taxonomy terms pages at:

  • /blog/categories/
  • /portfolio/categories/

For either, the terms template gets only its own terms from .Data.Terms, and the values are independent:

  • /blog/categories/category-1/
  • /portfolio/categories/category-1/

Instead of hardcoding the taxonomy “plural” name (“blog/categories”) to the template, you could even store it in section _index.md page params. Then you don’t need a separate template. Otherwise you can probably keep the layout DRY by using template blocks and partials. The taxonomy and terms templates can be defined separately for sections with the help of partials.

If you want to add metadata to terms, you can do it the normal way - just create a file: /blog/categories/category-1/_index.md and put the title and other params to its front-matter.

If needed, I can provide a minimal working example

2 Likes

Thank you for your comprehensive and helpful response. I am slowly getting my head around the structure of Hugo and best practices and your response helped significantly.

1 Like

I did it like and got all of the categories in the section blog.

{{ $t := where .Site.Taxonomies.categories.ByCount ".Page.Section" "blog" }}
{{ range $t }}
  <li><a href="{{ .Page.Permalink }}">{{ .Page.Title }}</a></li>
{{ end }}

Replace the section blog to any you want to.

2 Likes

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