Taxonomy with non-array element from frontmatter?

Is it possible to configure taxonomies to work with non-array values in frontmatter?

For example, imagine the post:

---
year: 2001
categories: ["cata", "catb"]
---
post body

It is easy to configure taxonomies on the categories element of the frontmatter.

config.toml

[taxonomies]
  category = "categories"

However what if we also want a taxonomy on the year member of the frontmatter. In the context of this site it doesn’t make sense for the post to have more than one year.

If I change my config.toml to

[taxonomies]
  category = "categories" 
  year = "years"

no pages are listed in the taxonomy when I have this in my template:

{{range $key, $value := .Site.Taxonomies.years}}
	{{$key}}
{{end}}

Is there a way to use the years member of the frontmatter?

Have you tried year: ["2001"]? (That’s just a guess).

However, is there any specific use case for this? I think, you can already sort posts by date in Hugo and (I’m not sure), but, there must be some other way that could be used to group posts by year.

Edit: I just noticed, you’re using year in frontmatter, but, in config, you’ve set the word as years. Possibly another cause of an error.

I could change the year field to an array, but would prefer not to because I am using Netlify’s cms and it doesn’t make sense for a post to have multiple years.

I was hoping there would be a configuration setting or option to allow a single year to be used from the frontmatter.

This would be a hacky way and you (and even myself), won’t like it, but, can use used as a last resort. You could create a separate frontmatter field to be used by netlify CMS and the taxonomy.

If someone more knowledgeable answers with a better solution you’re good to go, but, if you want something quick, you can give that a try.

This should work. You should be able to define your frontmatter as

years: 2020
 OR 
years:
  - 2020
  • note the use of plural form

If you want to use “year” (singular) instead, define your taxonomies accordingly:

[taxonomies]
year = "year"
1 Like

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