Assign taxonomy to all files in a (sub)directory

Hi,

is there a way to assign a taxonomy (e.g. “tag”) to all files in a folder - so if “a.md”, “b.md” and “c.md” are all located in a folder “projects”, I’d like to tag them as “project” without having to add

tag:
- project

to each single file?

I tried playing around with “_index.md”, but this doesn’t seem to work …

The approach was right.

In the folder’s _index.md add this to the frontmatter:

cascade:
  tag:
  - project

The problem might be that frontmatter tag in sub-items might override that.

More about cascades

Hi,
I added your suggestion to the “_index.md”, but when accessing the tags with

{{ with .Params.Tag }}
{{ . }}
{{ end }}

the tag added in the “_index.md” doesn’t appear, even if I remove the

tag:

from the frontmatter of my test-md-file.

It could be that the proper cascade is this:

cascade:
  params:
    tag:
    - project

The documentation seems to require that the items be under params and your code uses .Params.tag so that might have been an oversight by me.

1 Like

Hi,

I haven’t tried the cascade for taxonomy terms, but the method to get a list of all taxonomy terms in a page object is .GetTerms. In your case .GetTerms("tags").

1 Like

Unless you have overridden Hugo’s default taxonomies, you should be using the word “tags” (plural) not “tag” (singular).

content/posts/_index.md

title: Posts
cascade:
  tags:
    - foo
    - bar

The above will add the tags “foo” and “bar” to descendent content that has not been assigned one or more tags.

HI,

thanks for the hint. Changed my code, but neither

cascade:
  tag:
  - project

nor

cascade:
  params:
    tag:
    - project

adds a tag from “_index.md” to the list on the page.

From what I see on the docs posted by @davidsneighbour this should work - so I’ll re-check my settings and maybe build a small test-site to check there’s nothing interfering from my current settings.

Hi,

found the problem - @jmooring: I did indeed overwrite the default taxonomies, which caused some trouble here. :frowning_face:

From what I see in the docs (and played around with now that it’s working), the moment I add tags to the frontmatter of the page, the cascaded values are gone and I there is no way to keep the cascaded tags and add some additional tags using the frontmatter of the page.

That is correct. Cascade fills in blanks; it does override or add elements to existing arrays.

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