.Site.Taxonomies Categories and Tags

I am trying to separate tags and categories while I call the following function.

Following works to define the tags and categories.

 {{ if .Data.Singular }}
 ...
 {{ end }}

I have tried .Site.Taxonomies.categories and .Site.Taxonomies.tags but they dont seem to work. They are defined on my config.toml file as follows.

[taxonomies]
  category = "categories"
  tags = "tags"

Can someone point me in the right direction?

I am also trying to add the params on the config.toml file to enable and disable this function but somehow it doesn’t seem to work…

{{ with .Params.taxnoindex }}
        {{ if .Data.Singular }}
            ...
        {{ end }}
{{ end }}

Hi,

I don’t really understand what you are trying to do. Where are you putting this code? What do you want it to do? Are you trying to list all the tags? Or all the tags for one content page?

Have a look at the examples from docs here: https://gohugo.io/templates/taxonomy-templates

If you are trying to list categories and tags then you can follow those codes

Categories

{{- if isset .Site.Taxonomies "categories" }}
{{- if not (eq (len .Site.Taxonomies.categories) 0) }}
<ul>
  {{- range $name, $items := .Site.Taxonomies.categories }}
  <li><a href="{{ "categories/" | relLangURL }}{{ $name | urlize | lower }}">{{ $name | title | humanize }}</a></li>
  {{- end }}
</ul>
{{- end }}
{{- end }}

Tags

{{- if isset .Site.Taxonomies "tags" }}
{{- if not (eq (len .Site.Taxonomies.tags) 0) }}
<ul>
  {{- range $name, $items := .Site.Taxonomies.tags }}
  <li><a href="{{ "tags/" | relLangURL }}{{ $name | urlize | lower }}">{{ $name | humanize }}</a></li>
  {{- end }}
</ul>
{{- end }}
{{- end }}

And you don’t need to define anything in config.toml file.

On the head.html, I am trying to add a meta info content=“noindex” to tags and category pages. Currently, I got it to work using the following info code which works for both tags and categories.

{{ if .Data.Singular }}
…
{{ end }}

What I am trying to do is to separate tags and categories and have an option to turn them on/off using individually at the config.toml file.

I hope I have explained it correctly.

You could check if the .Page .Kind is taxonomy or taxonomyTerm ( {{if eq .Kind "taxonomy" }} ) :
Page variables | Hugo

You could also define custom templates: Template lookup order | Hugo

I still don’t understand what this means. Do you mean you want to have a separate layout for /tags/ and /categories/ pages? Or do you want a tags layout that is different from a categories layout? If so have a read at the docs on lookup order I link to above.

You can disable taxonomies: Taxonomies | Hugo
and remove individual ones: Taxonomies | Hugo


It might be easier if you have a repo you can share to explain what you are trying to do.

Thank you for pointing me in the right direction. This is what I was able to do to separate tags and categories.

{{ if eq .Data.Singular .Kind "tags" }}<meta name="robots" content="noindex" />{{ end }}
{{ if eq .Data.Singular .Kind "category" }}<meta name="robots" content="noindex" />{{ end }}

I need to find a way to enable/disable each statement via the config.toml file.

2 Likes