Hugo Shortcode: Taglist
I needed a Hugo shortcode to show a list of posts that belonged to a certain tag, which is quite easy. Add the following to layouts/shortcodes/taglist.html
or whatever name you like to use for the shortcode.
1{{- $title := $.Params.title -}}
2{{- $tag := $.Params.tag -}}
3{{- $limit := int ($.Params.limit) -}}
4{{- $tags := (index site.Taxonomies.tags $tag) -}}
5{{- if (eq $limit -1) -}}
6 {{- $limit = len site.Taxonomies.tags -}}
7{{- end -}}
8<h3>{{- $title| default "Related Posts" -}}</h3>
9<ul>
10{{- range (first $limit $tags.Pages) -}}
11 <li>
12 <a href="{{- .Permalink -}}">{{- .Title -}}</a>
13 </li>
14{{- end -}}
15</ul>
The available parameters for this shortcode are:
param | type | description |
---|---|---|
title | string | title of the list output, will be put inside of h3 tags |
tag | string | tag name whose items are to be shown |
limit | int | number of items to show, -1 for all |
Now calling it via one of the following shortcodes will show a list of recent posts in that tag:
This post is part of #100DaysToOffload 48 posts since Dec 04, 2022 — 85 total posts