Hugo iterate pages of section

By

Learn how to iterate over the pages of a specific section in Hugo using range and a where filter on .Site.Pages to loop the markdown files in that folder.

~~~

I had the need to iterate over the pages of a specific section in Hugo, which means markdown files put inside a folder under content, like content/mysection.

On a section page, iterate over that section’s regular pages with .RegularPages:

{{ range .RegularPages }}
  <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}

If you need the pages from mysection while rendering a different page, first ask the site for that section:

{{ with site.GetPage "/mysection" }}
  {{ range .RegularPages }}
    <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
  {{ end }}
{{ end }}

Use .Pages instead when you also want nested section pages in the result. Hugo applies the section’s default sort order unless you choose a different page collection.

Tagged: Hugo · All topics
~~~

Related posts about hugo: