How can I list all pages in a sub-folder?

I would like to know how I could list all pages in a sub-folder using Hugo. This is how my content folder is structured:

content\
--- general\
------ file1.md
------ file2.md
------ file3.md

And now I would like to access file1, file2 and file3 in the main list.html file which is located under layouts/_default . I tried using things like:

{{range where .Site.Pages "File.Dir" "general"}}

and

{{range where .Site.Pages "Section" "general"}}

as well as both combinations using general/ . I also tried a few other solutions from similar topics, but surprisingly, none of them worked at all.

However neither of them seems to work.

Try .Site.RegularPages

1 Like

Thank you very much for your answer. This seems to work fine. I suppose I forgot to use this combination, because I tried something with .Site.RegularPages before, but I wasn’t sure how exactly this worked.
If anyone is interested in the exact answer…
I had to use the second example with .Site.RegularPages and “Section”:

{{range where .Site.RegularPages "Section" "general"}}

What I would recommend is you do something ala:

{{ $general := site.GetPage "general" }}

{{ range $general.RegularPages }}

{{ end }}

2 Likes

Thank you for the recommendation. This looks quite clean and intuitive to use. I’ll consider this suggestion with great pleasure.

1 Like

A related tip:

{{ $general := site.GetPage "general" }}

{{ range $general.RegularPagesRecursive }}

{{ end }}

The above will recurse into any subsections.

Also, note that I had a typo in my first example (corrected now).

1 Like

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