Hello together,
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:
{{ $tag := .Get "tag"}}
<ul>
{{range where .Site.Pages ".Params.tags" "intersect" $tag }}
<li>
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
</li>
{{end}}
</ul>
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:
Okay, I understand. $.Page.Params.tags is the array of the tags of the pages and I want it to intersect with $tag .
Unfortunately I still don’t get any links listed by using
{{range where .Site.Pages "$.Page.Params.tags" intersect" "$tag"}} or
{{range where .Site.Pages "$.Page.Params.tags" intersect" (slice "$tag")}}
To clarify, you don’t have a context problem.
<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.
Your shortcode doesn’t work either. I always used tags in an array in my frontmatter
Edit: It does work! My mistake was using (slice "...") instead of (slice (...)).
thanks
Okay, now I am confused.
{{ range .Site.Pages }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
for example works.
Well, @jmooring is far more knowledgeable than I am, so I was probably wrong.