Hugo iterate pages of section
By Flavio
Published · Updated
Tagged: Hugo
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.
I tested everything below with Hugo 0.170.
Loop the pages on the section page
On a section page (the section template, layouts/section.html in current Hugo), iterate over that section’s regular pages with .RegularPages:
{{ range .RegularPages }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
Loop the pages of a section from any other page
If you need the pages from mysection while rendering a different page, like the home page or a sidebar partial, first ask the site for that section:
{{ with site.GetPage "/mysection" }}
{{ range .RegularPages }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
{{ end }}
site.GetPage returns nothing if the section doesn’t exist (for example if you mistype the name). The with block handles that: it just renders nothing instead of breaking the build.
Using where on the site pages
Another way is to filter all the site pages by their section:
{{ range where site.RegularPages "Section" "mysection" }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
Two things to know here.
Use site.RegularPages, not .Site.Pages. .Site.Pages also contains the list pages, so the section page itself (the one made by _index.md) and any nested section pages show up in the loop too.
Section is always the top-level folder. So this returns the pages of nested sections too, like content/mysection/series/part-1.md. That’s different from .RegularPages on the section, which skips them (more on this below).
What counts as a section
From the Hugo docs: a section is a top-level folder in content, or any folder that contains an _index.md file.
Take this structure:
content/
mysection/
_index.md
first-post.md
2025/
old-post.md
series/
_index.md
part-1.md
mysectionis a section because it’s a top-level folder (the_index.mdis optional here, you add it to give the list page a title and content)2025has no_index.md, so it’s not a section.old-post.mdbelongs tomysectionand shows up in its.RegularPagesserieshas an_index.md, so it’s a nested section.part-1.mddoes not show up in the.RegularPagesofmysection
RegularPages vs Pages vs RegularPagesRecursive
On the mysection page of the example above, this is what each one returns:
| Method | What it returns |
|---|---|
.RegularPages | first-post and old-post |
.Pages | first-post, old-post, and the series section page |
.RegularPagesRecursive | first-post, old-post, and part-1 |
So use .RegularPagesRecursive when you want every post under the folder, nested sections included:
{{ range .RegularPagesRecursive }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
Use .Pages when you want to list the nested sections as well, for example to build a list of series with their own links.
Sort the pages
By default Hugo sorts by weight first, then by date with the newest first. Pages without a weight come after the ones that have one.
If you want the newest first no matter the weight:
{{ range .RegularPages.ByDate.Reverse }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
Other sort methods are .ByTitle, .ByWeight, .ByLastmod and .ByPublishDate. They all sort in ascending order, add .Reverse to flip it.
Show only the latest 5 pages
Use first:
{{ range first 5 .RegularPages.ByDate.Reverse }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
Sort before you call first, otherwise you get the first 5 of the default order.
Group the pages by year
{{ range .RegularPages.GroupByDate "2006" }}
<h2>{{ .Key }}</h2>
{{ range .Pages }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
{{ end }}
"2006" is a Go date layout, not a real year. It means “format the date as a 4-digit year”. The groups come out with the most recent year first.
Paginate the section
For a long section, use .Paginate:
{{ range (.Paginate .RegularPages).Pages }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
{{ partial "pagination.html" . }}
pagination.html is a template that ships with Hugo, it prints the links to the other pages. Set how many pages go on each pager in hugo.toml:
[pagination]
pagerSize = 10
Hugo then creates /mysection/page/2/ and so on.
Pagination only works in the home, section, taxonomy and term templates, not on a single page. And call .Paginate once per template: Hugo caches the first call, and later calls reuse its result.
Why is a page missing?
If a page doesn’t show up in the loop, check these:
- it has
draft: truein the frontmatter. Drafts are excluded unless you runhugo server -D - it has a
datein the future. Hugo skips future pages unless you build with--buildFuture - it’s inside a nested folder that has an
_index.md, so it’s in a different section. Use.RegularPagesRecursive
If you’re just getting started, see how to start a blog using Hugo.
Want me to talk about your product? You can sponsor this site.