Loop through categories & tags

I want to list through categories & tags

  1. below code give me all tags across all pages. I want to list tags on one particular post only.

     <div>
                   {{ $tags := $.Site.Taxonomies.tags.ByCount }}
                   {{ $v1 := where $tags "Count" ">=" 1 }}
                   {{ $v2 := where $v1 "Term" "not in" (slice "hugo" "tags" "rss") }}
                   {{ range $v2 }}
                   {{ if .Term }}
                   {{ $tagURL := printf "tags/%s" .Term | relURL }}
                     <span class="btn btn-outline-info btn-sm" style="margin-bottom: 5px;">
                       {{ .Term }}
                     </span>
         {{ end }}
        {{ end }}
      </div>     
    
  2. how to list through all the categories like above code?

I use the following partial in all single templates, get what you need

{{if not .IsHome -}}
<div class="b no-print w-100">
{{if (ne .Type "menu") -}}<span class=nowrap><a href="/" title="{{ $.Scratch.Get "home" }}"><i class="fas fa-home fa-fw"></i></a>

{{ if isset .Params "categories" -}}
  {{ range $index, $name := sort .Params.categories -}}
	{{ with site.GetPage (printf "/%s" ($name | urlize )) -}} 
		<a class=ph1 href="{{ .RelPermalink }}" title="Bereich: {{ .Name | markdownify}}">{{ .Name | markdownify}}</a> 
	{{- end}}
  {{- end }}
{{ end }}
</span>

{{ if isset .Params "tags" -}}
	<span class=nowrap>
	<a href="/tag/" title="{{ $.Scratch.Get "index" }}"><i class="fas fa-tag fa-fw"></i></a>
	{{ range $index, $name := sort .Params.tags -}}
		<a class=ph1 href={{ (printf "/tag/%s/" $name) | urlize }} title="Index: {{$name | markdownify }}">{{$name | markdownify}}</a>
	{{- end}}
	</span>
{{- end }}

{{- end}}
</div>
{{- end}}
1 Like