How to add taxonomy metadata to the head (incl. Twitter Card and OpenGraph)

My first Hugo site is now online and I am still sorting out some quirks.

How to get taxonomy metadata into the head of the html-file? This also affects the Twitter card and OpenGraph metadata.

I found a solution, by adding a categories folder to the contents folder, including an file with a title and a description in the frontmatter for categories in general. Dito for tags. If I want to do that for every category and tag, do I have to create a folder and an file for each and every category and tag? Did I overlook a feature of Hugo?

Or is there another solution which involves adding some code to the head-partial and maybe using a data-file?

For finishing version 0.10 I am going to stick to creating a folder and an index-file for categories and tags in general, but for the next version I want a solution for individual categories and tags.


This depends on where you are getting your meta information, ie how it is getting defined. If, for example, you need to set a custom value for each category/tag, then yes you would need to create and for each one you want to configure.

That is something I really want to avoid. If I can test whether a page is a category or a tag page, that might be a better solution.

Something like:
titel = Category name - Blogname
description = Collection of blogpost for category name - Blogname

I think I might solve it with testing for .Data.Singular in the <head>. If not a taxonomy page it will be empty. Possible values category and tag. Needs some more testing.

Similar for .Data.Plural.

Edit 1: Still have to figure out how to differentiate between a taxonomy and a term page.

Edit 2: I found a workable solution to differentiate between a taxonomy and term page:

{{ if .Data.Terms }}
    <p>Taxonomy page: {{ len .Data.Terms }} terms</p>
{{ else }}
    <p>Term Page</p>
{{ end }}

If somebody has a better/other solution, please let me know.

You can check for the current .Page's .Kind: Page Variables | Hugo