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 }}
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.)
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
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}