Help me show number of tags in my widget

hello iam using this code to show tags on my website, but the code shows all the tags i only want to show a number of tags like 10 or 15 can someone please edit the code for me

{{- $tags := .Site.Taxonomies.tags }}
{{- if gt (len $tags) 0 }}
<div class="widget-taglist widget">
	<h4 class="widget__title">{{ T "tags_title" }}</h4>
	<div class="widget__content">
	{{- range $name, $taxonomy := $tags }}
		{{- with $.Site.GetPage (printf "/tags/%s" $name) }}
		<a class="widget-taglist__link widget__link btn" href="{{ .RelPermalink }}" title="{{ .Title }}">
			{{- .Title -}}{{- if .Site.Params.widgets.tags_counter }} ({{ $taxonomy.Count }}){{ end -}}
		</a>
		{{- end }}
	{{- end }}
	</div>
</div>
{{- end }}

This should help

1 Like

If I understand your problem correctly, you would barely need the code above.

Assume you only need to display a widget telling visitors how many tags your site has. In that case, you only need len function. len function will always return the slice/array size in number. Therefore we have this:

{{- $numberOfTags := len .Site.Taxonomies.tags }}
{{- if gt $numberOfTags 0 }}
	<aside class='widget-taglist widget'>
		<h4>{{ T "tags_title" }}</h4>
		<div class='widget__content'>
			{{ T "number_of_tags" (dict "Count" $numberOfTags) }}
		</div>
	</aside>
{{- end }}

I would recommend you set it in multi-languages function T "number_of_tags". Remember to pass it with dict "Count", it will be helpful to display singular tag (if that is the case you need.)

1 Like

thank you for responding to my problem, i think you didn’t understand me because of my bad english, i i do not want to show the number of tags ( how many tags i have ) i want to show only 10 last used tags or 10 random tags etc… any order is okay .and thank you again

Changing this line above should do the work:

{{ range $name, $taxonomy := first 10 $tags }}

It’s a similar case if you were trying to limit a list of Articles:

{{ $articles:= where .Site.Pages "Section" "articles" }}
{{ range $index, $article:= first 5 $articles}}
{{ if (eq $index 0) }}
{/* do some with first title */}
{{ $article.Title}}
{{ else }}
{ $article.Title}}
{{ end }}
{{end}
1 Like

after adding the code above i got this error

ERROR 2020/08/31 10:42:32 render of "page" failed: execute of template failed: template: _default/single.html:43:6: executing "_default/single.html" at <partial "sidebar.htm...>: error calling partial: "/opt/build/repo/themes/Feed/layouts/partials/sidebar.html:6:31": execute of template failed: template: partials/sidebar.html:16:6: executing "partials/sidebar.html" at <partial $p $root>: error calling partial: "/opt/build/repo/themes/Feed/layouts/partials/widgets/taglist.html:6:31": execute of template failed: template: partials/widgets/taglist.html:6:31: executing "partials/widgets/taglist.html" at <first 10 $tags>: error calling first: can't iterate over hugolib.Taxonomy

My bad, forgot that you can’t do a limit on a map.
Try this approach instead:

i have tried that but the widget didn’t show in my sidebar at all

Can you share a basic version of the project on GitHub so I can debug it locally?

1 Like

sure. here is my repo : xxx
the file responsible for the tags widget is located here : xxx

Here you go. :slight_smile:


{{- $.Scratch.Set "myLimit" 0 }}

{{- $tags := .Site.Taxonomies.tags }}

<div class="widget-taglist widget">

    <h4 class="widget__title">{{ T "tags_title" }}</h4>

    <div class="widget__content">

    {{- range $name, $taxonomy := $tags }}

        {{- $.Scratch.Set "myLimit" (add ($.Scratch.Get "myLimit") 1) }}

        {{- if le ($.Scratch.Get "myLimit") 10 }}

            {{- with $.Site.GetPage (printf "/tags/%s" $name) }}

            <a class="widget-taglist__link widget__link btn" href="{{ .RelPermalink }}" title="{{ .Title }}">

                {{- .Title -}}{{- if .Site.Params.widgets.tags_counter }} ({{ $taxonomy.Count }}){{ end -}}

            </a>

            {{- end }}

        {{- end }}

    {{- end }}

    </div>

</div>

1 Like

thank you very much this exactly what i wanted :slight_smile:

You’re welcome! :slight_smile:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.