On the home page of a Hugo site, I’m trying to get list.html to show the three pages from a specific sub-directory: /content/index/companies - however, I’m not getting a single page with the code below. I can’t get my head wrapped around how I should be thinking about range over Sections.
<ul>
{{ range .Pages }}
{{ range where .Pages "Section" "index" }}
{{ range first 3 (where .Pages "Section" "companies" ) }}
<li>Title: {{.Title}}</li>
{{end}}{{end}}{{end}}
</ul>
I originally tried with just the section I was looking for, but this doesn’t work either:
{{ range where .Pages “Section” “companies” }} {{ .Title }} {{ end }}
In /content/index/companies I have an _index.md file as well as several .md-pages.
What would be the right way to do this? Thank you for any tips!
I am also curious to know of alternative ways because the docs about sub-directories/nested sections are/were very few. But the way I was able to do it in my site was through a front matter cascade based on a suggestion from searching this forum. Assuming you are using YAML, add the code below to the companies folder’s _index.md file.
cascade:
type: companies
Then your code should look like below.
<ul>
{{- $pages := where site.RegularPages "Params.type" "companies" }}
{{- range $pages | first 3 }}
<li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
{{- end }}
<ul>
Thank you! Setting type with cascade and fetching pages based on that also worked for me. Curious about how to take this a step forward: if I have several types and want to loop over them with range, is there any way to get all “site.RegularPages.Params.type” and then do a range over them - how would you do that? Cheers!