Add meta-tags to taxonomy page

How add the metatags to taxonomy page ?
For example, https://gcore.lu/category/cdn/
Google said, that pages does not have metatags.

The description meta tag as you probably know goes in the head of a document.

Taxonomies use either list.html or taxonomy.html if you have created it under layouts/_default/ or if you have specified a template per taxonomy term the template is typically under/layouts/taxonomy/SINGULAR.html

See here for the template lookup order https://gohugo.io/templates/list/

You can create _index.md for both your taxonomies and your terms the same way you would for content sections. Said files can contain both content and front matter.

1 Like

Of course. But his source is missing the meta description tag altogether from the head.

I assume the author of this thread knew how to create the corresponding templates since the page is otherwise rendering. To add metadata and content to these templates for taxonomies, he will need to create the _index.md pages

Yes. But making an _index.md per taxonomy as well as replicating the taxonomy URL as a folder tree is a bit of an overkill IMHO.

EDIT
The OP can check for taxonomy terms in the template and add meta accordingly.

Yes, I would like to somehow just solve everything.
You need to generate meta by template for Taxonomy.
For example, you need some condition in the

{{ if eq Taxonomy }} {{ .TaxonomyName }} {{ end }}

How to set this condition correctly?

The taxonomies model is different than the section model in Hugo.

All taxonomies variables refer to the terms list.

There is currently no way to make the condition work as you’re asking. Unless of course @bep knows of another way.

The only way to do it (without going overboard with replicating a taxonomy URL in a folder tree and _index.md) is to create a template for each taxonomy term under /layouts/taxonomy/SINGULAR.html (e.g. /layouts/taxonomy/topic.html). Again see the template lookup order in the Docs (I pasted the link in an earlier reply)

Use block templates for your head partial and in each term’s template change the meta description as you want. See https://gohugo.io/templates/blocks

BTW it’s these limitations in taxonomies that have made me rely on sections for my Hugo projects. Usually I turn off all taxonomies in my config and use tags for internal purposes.

On a rendered page like yoursite.com/tags, which could be layouts/_default/terms.html:

{{.Kind}}
=>  "taxonomyTerm"

On a rendered page like yoursite.com/tags/foo, which could be from layouts/_default/taxonomy.html or layouts/_default/list.html

{{.Kind}}
=> "taxonomy"

You can check for these returned values…

{{ if eq .Kind "taxonomyTerm"}} codey code code...{{end}}

Etc…

1 Like

Just tested this and I’m afraid that I cannot get {{ .Kind }} to work like you suggested. {{ .Kind }} is context aware obviously, hence it is not possible to use it for changing a taxonomy page’s meta description from within the head partial.

Also tested {{ .Kind }} from within layouts/_default/taxonomy.html and again no luck.

BUT your reply got me thinking @rdwatters and thank you for your input.

So @igramnet discard my previous reply. Here is how I solved this issue:

Inside your head.html partial put the following:

<meta name="description" content='{{ if eq .URL "/tags/my-tag/" }}Blah blah blah{{ end }}'/>

Repeat the check for different taxonomies URLs and that’s all there is to it.

I would not recommend checking for hardcoded strings…

Just tested the following at layouts/partials/head.html:

{{ if eq .Kind "taxonomyTerm"}}
<meta name="testing" content="This is a taxonomyTerm page">
{{ else if eq .Kind "taxonomy" }}
<meta name="testing" content="This is a taxonomy page">
{{ end }}

Here it is running locally…

Yes. You’ve said so before. But why? It doesn’t break building the site.

Anyway thank you for your reply. But I think the issue here is not just about making a generic check for Taxonomy pages but a check for specific terms like for example check for a tag called News that can be found under /tags/news/

I have been trying to make this work for specific terms, from the start. But I simply cannot make a successful check with the Taxonomy Variables (unless I’m missing something) hence the .URL workaround.

Because hard coding defeats the convenience of abstraction that templating allows you. If you are looking to have a site with two tags, your example would be a-okay, but otherwise you’re talking about setting up a hundred if/else statements in your templating. You can also use default in the examples I provided…

1 Like