Shortcode for listing a taxonomy's pages

Hello - I’d like to have a shortcode to render a list of pages from a taxonomy, but I cannot seem to make it work. My usage idea is to specify the shortcode wherever on a markdown page, to create a bullet list of all the pages assigned to some taxonomy.

I tried this shortcode:

{{ "<!-- ENTERING layouts/shortcodes/list-taxo.html -->" | safeHTML }}
{{ $myterm := .Get "term" }}
{{ $mytopicpages := index $.Site.Taxonomies.topics $myterm }}
{{ range $mytopicpages.Pages }}
<li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
{{ end }}
{{ "<!-- LEAVING layouts/shortcodes/list-taxo.html -->" | safeHTML }}

… calling it like this in a markdown content file:

{{< list-taxo term="Services" >}}

… but get an error:

ERROR: 2015/07/27 error processing shortcode shortcodes/list-taxo.html
ERR: template: shortcodes/list-taxo.html:4:27: executing "shortcodes/list-taxo.html" at <$>: Site is not a field of struct type *hugolib.ShortcodeWithPage
WARN: 2015/07/27 &{map[term:Services]  } 

I tried changing the $.Site to .Site, but that also just gives the same basic error.

Somehow the page cannot “see” $.Site or .Site. Can anyone point me in the right direction on this?


Hugo Static Site Generator v0.15-DEV-90AF334 BuildDate: 2015-07-11T20:05:33+09:00


… but I’m not sure this will work, even with the correct syntax. The chicken and the egg and all. I’m pondering on a solution to this issue, as several are asking for it; but don’t expect it today – the hardest part is proving Einstein wrong.

Thanks @bep! Ah, I see. How can Hugo know all the pages to list in the middle of building the pages to be listed.

What about auto-generating a big “map” file containing titles and urls, in the data folder, which is then parsible using index from the shortcode. I guess timing is an issue here, too.

Well, the titles and the URLs and similar are easy (and maybe already there? If not, that is my goal.). But for .Content and .WordCount etc. you kind of have to take the shortcode itself into account. And that is the branch you’re sitting on.

My particular use case just needs the title and url, but, I can see where someone might want to excerpt something else from the page, in say, a list of related content or something like that.

By the way I did try:


But as you alluded, it does give an error, FYI:

ERROR: 2015/07/28 error processing shortcode shortcodes/list-taxo.html
 ERR: reflect: call of reflect.Value.Type on zero Value
WARN: 2015/07/28 &{map[term:testing]  }
INFO: 2015/07/28 found taxonomies: map[string]string{"series":"series", "tag":"tags", "topic":"topics"}

@RickCogley Did you find a solution to this? I’d really like to achieve the same thing:

  1. A shortcode that can be used on markdown pages
  2. Pass the taxonomy term into the shortcode
  3. Pass in a number to limit the number or returned items
1 Like

I too would like a solution to this issue. Any updates?