How to List All Pages of a Section in Hugo
Asked Answered
A

1

5

I'm trying to list pages from a section on the front pages. This shouldn't be too hard; for example, with the following hierarchy (names genericised):

/content/
        |_ test-page.md
        |_ /section-name/
                        |_ page1.md
                        |_ page2.md

Using the following loop:

{{ range .Pages }}
   ...
{{ end }}

Gives the list page for /section-name/ as "section-names".

OK, well, I looked online for other solutions. For example:

{{ range first 5 (where .Data.Pages "Section" "in" (slice "section-name")) }}

But this still returns "section-names".

OK, so the documentation has example of where clauses and grouping. For example, {{ range (.Pages.GroupBy "Section").Reverse }}. Let's try a similar concept:

{{ range (.Pages.GroupBy "Section") }}
   {{ .Key }}

   {{ range .Pages }}

      {{ .Title }}

   {{ end }}
{{ end }}

This seems like it would print every key, then print every page under that key. However, it prints section-name Section-names instead of what I was hoping for, section-name page1 page2.

What?

Also, using a simple loop like {{ range .Pages }}, why does test not appear as soon as a section folder is created? I don't understand Hugo's seeming obstination to show me just the section page.

Adrell answered 24/11, 2020 at 23:38 Comment(0)
A
8

This worked:

{{ range (where .Site.Pages "Section" "programming") }}
   {{ range .Pages }}
      {{ .Title }}
   {{ end }}
{{ end }}
Adrell answered 24/11, 2020 at 23:51 Comment(1)
In the hugo config file you can set pluralizeListTitles to false discourse.gohugo.io/t/…Nihhi

© 2022 - 2025 — McMap. All rights reserved.