Add parent directory name to a taxonomy

I want to create a single taxonomy that is essentially the union of a page’s “parent directory name” and keywords (specified in front matter, if any). In other words: I want the directory to act as an implicit keyword.

Example with two posts (under “content/” root):

  • “posts/hugo/about-hugo.md” (with no keywords specified in front matter)
  • “posts/debugging/debug-hugo.md” (with “keywords: [hugo]” in front matter)

In this case, I want both posts to be in a taxonomy under the term “hugo” (the first due to it being in the “hugo” directory, and the second because it’s explicitly marked with “hugo” as a keyword in front matter).

Let me know if it’s not clear what I’m trying to achieve!

Is there any way to do this in Hugo?

The taxonomy docs make me think that taxonomy terms can only be added using front matter. If that’s the case, is there anyway to programmatically recreate taxonomy-like behavior using variables and functions?

I’ve been playing around with the slices/append, and it looks like I might be able to create an array and/or map that contains taxonomy-like information (not 100% sure this is possible), and maybe I can store that in .Scratch for use across my templates (also note 100% sure this would work), but I’m unsure how to make it generate taxonomy term pages (in fact, I think this is not possible, based on the Build pages from data source open issue).

So from what I’ve read thus far, I suspect this is not possible, but I’d love to be proven wrong :slight_smile:

Edit to add: Ideally, I wouldn’t have to clutter up my content with manually-generated files. I say this because I thought it might have been possible to set an explicit keyword in “_index.md” of each directory and cascade it down if necessary. Note: I haven’t gotten that approach to work (edit: because the “keywords” property seems to get overwritten entirely instead of merged), so it’s possible that’s a dead end anyway.

No, there is not.

You are correct.

I think you are creating complexity.

Thanks! Yes, my situation might be unusual in that I already have a workflow I like and I’m investigating to see if Hugo can accommodate it. Being able to implicitly categorize content based on path is handy, but I could probably live without it.

Just be clear, you can cascade a taxonomy term down the tree from either a section page (_index.md) or from site configuration, provided that the term is not applied to the top most section—you would end up clobbering pages during site build.

But, as you have found, the taxonomy terms specified in front matter take precedence, as they should.

Right, I wanted it to merge the two arrays, but I agree that that is probably not the right thing to do in general (e.g. what order would that be done in? What about duplicates? Etc.), so the current behavior makes sense.

1 Like

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