Range over chosen .Site.Pages

Hi,
every my post has ID in front matter (1, 2,…24, 25, etc.), for example:

---
Title: Some interesting post title
id: 5
---

And I want to iterate over chosen posts by id
For example (this one works):

{{range where .Site.Pages ".Params.id" 5 }}
<li>{{.Title}}</li>
{{ end }}

BUT I want to iterate over more chosen post ids, something like this:

{{range where .Site.Pages ".Params.id" [1, 2, 5, 14, 24] }}
<li>{{.Title}}</li>
{{ end }}

Thank you for your help and advice.

Try

{{range where site.Pages ".Params.id"  "in" (slice 1, 2, 5, 14, 24) }}

{{ end }}
1 Like

Thank you @bep
This modified code works:

{{range where site.Pages ".Params.id" "in" (slice 1 2 5 14 24) }}

{{ end }}

For interest, I need to choose “post id” in Content (via shortcode).
So I put shortcode to .md content :

{{< chosen-posts "1 2 5 14 24" >}}

and shortocode chosen-post.html looks like:

{{ $shortcodeValues := split (.Get 0) " "}}

{{ range $shortcodeValues }}
    {{ $.Scratch.Add "valInt" (slice (int .))}}
{{ end }}

{{range where site.Pages ".Params.id" "in" ($.Scratch.Get "valInt") }}
<li>{{.Page.Params.name}}</li>
{{ end }}
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.