I want to create a shortcode to which I can give a tag and it will display all pages with that tag as a html link list. My current approach looks like this:
So I want to recall it with something like {{< shortcode tag=“mytag” >}}
But this doesn’t seem to work and I don’t know why. Could anyone tell me what I did wrong here?
I also tried using (slice $tag) instead of $tag with “intersect” but that doesn’t work either. I would appreciate some help.
First off, intersect expects two slices. But I guess your context is wrong: dot is not what you think it is. For example, .Params.tags should probably be $.Page.Params.tags.
Checkout the shortcode description in the documentation:
<ul>
{{ range where .Site.Pages "Params.tags" "intersect" (slice (.Get 0)) }}
<li>
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
</li>
{{ end }}
</ul>
Call it with:
{{< pages-with-tag foo >}}
The reason it didn’t work for you, I suspect, is that some/all of your front matter tags fields are scalar instead of arrays.
You must do this:
tags = ['foo']
Not this:
tags = 'foo'
Try it:
git clone --single-branch -b hugo-forum-topic-48427 https://github.com/jmooring/hugo-testing hugo-forum-topic-48427
cd hugo-forum-topic-48427
hugo server
Please read the documentation and my post again carefully. Your dot is not what you think it is anywhere in your code – .Site.Pages probably returns nothing, either.
And there’s no point in trying to intersect a slice with a single value.