How can I add the name of a category or a tag to the body tag? I’d like to do some specific CSS on single posts and taxonomy pages.
Right now, in base.html
, I use
<body class="{{ .Type }} {{ with .File }}{{ .TranslationBaseName }}{{ end }}">
which gives me
<body class="posts test-post-copy-2">
on single posts and either
<body class="categories">
or
<body class="tags">
on those taxonomy list pages.
How can I add the actual category or tag name to the body tag? I.e., for single posts, how can I show
<body class="posts my-category-name test-post-copy-2">
or
<body class="posts my-tag-name test-post-copy-2">
and on taxonomy list pages show
<body class="categories my-category-name">
or
<body class="tags my-tag-name">
maiki
April 25, 2020, 11:48pm
2
<body class="{{ .Type }} {{ .Title }}
or similar.
For listing the tags in single pages you could adapt the example at https://gohugo.io/templates/taxonomy-templates/#example-list-tags-in-a-single-page-template into a variable and split as needed for your classes.
Thanks, that makes sense; from that link I found that
{{ range (.GetTerms “tags”) }}{{ lower .LinkTitle }} {{ end }}
{{ range (.GetTerms “categories”) }}{{ lower .LinkTitle }}{{ end }}
outputs the tags and categories.
One problem is that I have many terms that are two words; is there a way to loop through the terms and replace the space in between the two words of each term with a dash?
That works:
{{ range (.GetTerms “tags”) }}{{ urlize .LinkTitle }} {{ end }}
The .Title isn’t much use for the body tag unless it is only used for taxonomy archives:
{{ if eq .Kind “taxonomy” }}{{ urlize .Title }}{{ end }}">
system
Closed
April 28, 2020, 3:02pm
6
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.