Range pages in sub-directory

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.

  {{ range .Pages }}
   {{ range where .Pages "Section" "index"  }}
     {{ range first 3  (where .Pages "Section" "companies" )  }}
    <li>Title: {{.Title}}</li>

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.

  type: companies

Then your code should look like below.

{{- $pages := where site.RegularPages "Params.type" "companies" }}
{{- range $pages | first 3 }}
<li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
{{- end }}
1 Like

Assuming you have created content/index/companies/_index.md

{{ range (site.GetPage "index/companies").Pages }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}
1 Like

Thank you! I took this a step forward and looped over all sub-directories in index with:

{{ range (site.GetPage "index").Pages }}
  <ul>{{ range .RegularPages }}
    <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
  {{ end }}</ul>
{{ end }}

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!

{{ range where site.RegularPages "Type" "in" (slice "books" "albums") }}
1 Like

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