Ranging through specific posts/pages

Hi all. A complete novice to Hugo here and I’m struggling a little understanding on how to range through specific post types.

I have a fictitious website that has the following pages…

  • Home
  • Product
    – product A
    – product b
    – product c
  • Blog
    – blog post a
    – blog post b
    – blog post c
  • Contact

On my index page, I have a section for products and a section for blog. So I would like to range through the products and select the “featured” products.

I also need to range through three latest blog posts.

I think my biggest issue right now is understanding how to tell Hugo to select specific pages.

I would have though… {{ range .Site.Blog }} and {{ range .Site.Products }}

Clearly that’s not right. Should I be using “type” in the front matter?

Thanks

Hi,

You will need to use where in your range: https://gohugo.io/functions/where/

Thanks @pointyfar will have a look at the link

So confused here!

Read through all the docs, and I can sort of understand if I wanted to display a specific post type using where. So if I had a the following,

type = “post”

and then a second

type = “product”

then I would use the following to call pages that are product pages right?

{{ range where .Site.Pages “type” “product”}}
{{ content }}
{{ end }}

Close but not quite there.
So Product and Blog in your example are “Sections” in Hugo-land. Which means you could do this:

{{ range where .Pages "Section" "Product" }}
2 Likes

Great, thanks for the help!