Skip to main content
Photo by Joanna Kosinska via Unsplash

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>
10{{- range (first $limit $tags.Pages) -}}
11  <li>
12    <a href="{{- .Permalink -}}">{{- .Title -}}</a>
13  </li>
14{{- end -}}

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:

1{{< taglist tag="tagname" limit="5" >}} --- showing 5 items with the default title
2{{< taglist tag="tagname" title="Some Title" >}} --- showing all items with the custom title
3{{< taglist tag="tagname" limit="-1" >}} --- showing all items with the default title
Back to top