Reputation: 303
I have a switch and want to add an if
statement for taxonomies but don't know how to call it? I tried if .IsTaxonomy
but get an error...
{{ if eq .Type "blog" }}
{{ .Title }}
{{ end }}
{{ if eq .Type "help" }}
{{ .Title }}
{{ end }}
{{ if eq .Type "reviews" }}
{{ .Title }}
{{ end }}
{{ if .IsHome }}
home
{{ else if eq .Type "page" }}
{{ .Title }}
{{ end }}
Upvotes: 3
Views: 784
Reputation: 1852
I have a switch and want to add an if statement for taxonomies but don't know how to call it? I tried if .IsTaxonomy but get an error...
The .Type
variable that you use with your if statements is something that Hugo gets from the content folder (more precisely the section). So your posts stored in /content/tutorial/
get the tutorial
type. You can also set the type of a piece of content by hand. But .Type
does not by default equal the content's taxonomy.
An alternative is to use Hugo's .IsNode
page variable -- that one always returns true
when the current page is a list page. That is, a page with posts from a certain taxonomy or section.
You can inspect the page's .RelPermalink
variable to see if the current page contains some taxonomy name (like "reviews"
). But I would advise against that, since it isn't a good practice. Any taxonomy change you make or new taxonomy means your theme's code need to be changed. Plus it also requires that you (or your users) never make a spelling mistake with taxonomy names, since else the theme's code breaks.
If I look at your if statements code, the following seems to be the equivalent of what you're trying to do:
{{ if .IsNode }}
<!-- Taxonomy and section list pages -->
{{ .Title }}
{{ else if .IsPage }}
<!-- Content pages -->
{{ else if .IsHome }}
<!-- Homepage -->
home
{{ else }}
<!-- All other pages, like the 404 page -->
{{ end }}
Upvotes: 4