Show 3 first pages for given category that unlisted param is not true

What I want to achieve:

Show 3 latest articles for given category. Only show the posts that don’t have unlisted: true param.

This works:

{{ with .Site.Taxonomies.category.marketing  }}
    {{ range first 3 . }}
        <a href="{{ .Permalink }}">{{ .Title }}</a><br>
    {{ end }}
{{ end }}

However, what I am missing is to filter this and show only articles in given category that don’t have unlisted: true in their front matter.

I tried this, but it does not work - I am getting error “error calling where: can’t iterate over .Params.unlisted”

{{ with .Site.Taxonomies.category.marketing  }}
    {{ range first 3 (where ".Params.unlisted" "!=" true) }}
        <a href="{{ .Permalink }}">{{ .Title }}</a><br>
    {{ end }}
{{ end }}

Help!

The above does not work because you missed the dot, therefore the Page Collection is not invoked:
{{ range first 3 . }} = Dot
{{ range first 3 (where ".Params.unlisted" "!=" true) }} = No dot

First of all store the context in a variable like so:

{{ with .Site.Taxonomies.category.marketing  }}
{{ $pages := . }}

And then rewrite the where statement to something like:

{{ range first 3 (where (where $pages) ".Params.unlisted" "!=" true) }}

You need to nest complex where statements.

Also have a look at: Context (aka “the dot”)

This does not work.

When I follow this suggestion and do:

{{ with .Site.Taxonomies.category.marketing  }}
{{ $pages := . }}

  {{ range first 3 (where (where $pages) ".Params.unlisted" "!=" true) }}

    <a href="{{ .Permalink }}">{{ .Title }}</a><br>

  {{ end }}
{{ end }}

I am getting error: “wrong number of args for where: want at least 2 got 1”.

Right. My bad. Should be:

{{ range first 3 (where $pages ".Params.unlisted" "!=" true) }}`

This topic was automatically closed after 4 days. New replies are no longer allowed.