List down posts from multiple categories

I want to be able to list down blog posts from different categories. I want to show 10 posts across all categories (combined, not 10 posts from each category) and I dont want to display those posts that have unlisted set to true.

So something like:

{{ with .Site.Taxonomies.category "new york" "tokyo" "london"  }}
{{ $pages := . }}
  {{ range first 10 (where $pages ".Params.unlisted" "!=" true) }}

    {{ partial "article-card.html" . }}

  {{ end }}
{{ end }}

I tried few approaches but I am failing at scoping this correctly.

Assuming your “blog posts” are under content/posts…

{{ $p := where site.RegularPages "Type" "posts" }}
{{ $p = where $p "Params.categories" "intersect" (slice "new york" "tokyo" "london") }}
{{ $p = where $p "Params.unlisted" "ne" true }}

{{ range $p }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}
1 Like

That does not work. It still lists the latest blog posts, from all categories, not just those posts that have at least one of these categories.

What I am looking for is, if I have 3 blog posts:

  1. Post A with category “new york”
  2. Post B with category “tokyo”
  3. Post C with category “london”

I want to list all of them down since they have categories I defined in my scope.

I think your solution is if lets say one blog has multiple categories and you only want to list down those that include all these 3?

Yes, it does. Try it:

git clone --single-branch -b hugo-forum-topic-36701 hugo-forum-topic-36701
cd hugo-forum-topic-36701
hugo server

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