Imagine we’re in our single post template. We’d have access to the .Params.categories
Page variable.
{{- with .Params.categories -}}
<div class="post-categories">
<div>
Categories:
{{- range . -}}
{{ $cat := urlize . }}
{{ with $ctx.Site.GetPage (printf "/%s/%s" "categories" $slug ) }}
<a class="tag" href="{{ .Permalink }}">{{ .Title }}</a> |
{{- end }}
{{- end }}
</div>
</div>
{{- end -}}
So the first line is our check for some categories:
{{ with .Params.categories }}
And then we loop through the post categories, using the shorthand for the current context
{{ range . }}
And then what we want to do is check to see if there is a page for that current category (you might have tags which aren’t public)
so we define a varable called $cat
which is the result of running the current category name through Hugo’s urlize
function.
{{ $cat := urlize . }}
If a page exists
then Hugo will output this category using the .Permalink
and .Title
of the category in the current iteration of the loop.
{{ with $.Site.GetPage (printf "/%s/%s" "categories" $slug ) }}