Custom parameter for a taxonomy in front matter

I’m using hugo to create an archive of articles, videos, whatever. I would like to use taxonomies and add parameters to it, that are not the same as metadata in the taxonomy page.

For example: I would like to have a taxonomy for ‘persons’, and in an article’s front matter write something like this:

    persons:
      - name: Jordan
        params:
          role: author
      - name: Julian
        params:
          role: editor

In my example, each of this person would have his automatic page built by hugo, and then from the article’s template we could access the taxonomy terms with their params in the context of the page. It would be analogous to page’s resources, but this doesn’t work:

Invalid persons in "archives/this-is-a-test/index.md"

Is there a secret way to do that? I couldn’t find anything like this in the documentation nor this forum.

Hi there,

The theory should work, but I don’t quite follow how you are trying to do it. It’s easier to help if we can see your site code. Please read Requesting Help .

Thank you for your answer. I wrote a minimal example that you can find here: https://github.com/jordan-dlh/hugo-tests

Please note that in the interval i updated my version of hugo and tried to use the new way to get taxonomy terms in my template, alongside the old one.

Of interest here is the ‘archives’ section where I link to the example page and try to access the page’s categories (works) and tags with a ‘role’ parameter (doesn’t work but doesn’t break either contrary to my initial post).

Then the page, the template of which tries to access tags and categories with the two methods I now know of. I can only access something with the new method (.GetTerms), and it is not the term but the taxonomy itself!

And finally in the tags taxonomy page, there is no content shown, contrary to the categories page.

You need to create pages for the tags / categories for which you want to define metadata (eg role): https://gohugo.io/content-management/taxonomies#add-custom-metadata-to-a-taxonomy-term

Edit to add:

I misread your initial question. That is not how one is supposed to declare taxonomies related to content, see this: https://gohugo.io/content-management/taxonomies/#add-taxonomies-to-content

You add them as strings or arrays of strings:

tags: "foo"

or:

tags: 
- foo
- bar

and not as objects. You define their metadata as per the docs page I link to above.

In fact it is especially what I want to avoid. Here the ‘role’ is not an attribute of the taxonomy (for example ‘person’), it is an attribute of the relationship between the content and the taxonomy. A same ‘person’ may have different ‘roles’ in different content.

So I conclude that what I want to achieve is not possible. Is there another way I could realize something like this, that would not be too hackish?

You could do it as ‘regular’ frontmatter in your content:

tags: foo
tagsrole: 
  - foo:
    role: baz

so in tags/foo/, tagsrole should be accessible via .Params when you call the .Pages

Thanks a lot, it works. However it defeats the DRY principle and it’s a missed opportunity IMHO.

Here’s what I do in my article’s index.md:

---
title: Hello World
date: 2020-04-18
type: archive
tags:
  - Scott
tagsrole:
  Scott:
    role: author
categories:
  - test
  - whatever
---
Hello there!

And in my theme’s layouts/_default/list.html

{{ define "main" }}

{{ if .Title }}<h1>{{ .Title }}</h1>{{ end }}

{{ if .Content }}<article>{{ .Content }}</article>{{ end }}

{{ if .Pages }}
        <ul>
        {{ range .Pages }}
        {{ $page:=. }}
        <li>
                <a href="{{ .RelPermalink }}">{{ .Title }}</a>
                Tags:
                {{ range (.GetTerms "tags") }}
                        <a href="{{ .Permalink }}">{{ .LinkTitle }}</a>
                        {{ if $page.Params.tagsrole }}{{$mmap := index $page.Params.tagsrole .Title }}{{ $mmap.role }}{{ end }}
                {{ end }}
                Categories:
                {{ range (.GetTerms "categories") }}
                        <a href="{{ .Permalink }}">{{ .LinkTitle }}</a>
                {{ end }}
        </li>
        {{ end }}
        </ul>
{{ end }}

{{ end }}

I updated my code in github to reflect this.

I think I will repurpose this approach and make pointers to regular pages (not taxonomies), in order to remain DRY.

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