How to list all regularPages from a nested section

Consider the following structure: website/writing/essays/. The writing section also has ‘notes’, like this: website/writing/notes/. On website/writing/ I want to show the 5 most recent essays and the 5 most recent notes.

I’ve tried the following: {{ range first 5 (where .Site.RegularPages "Type" "in" "essays" }}. This however doesn’t return anything. {{ range first 5 (where .Site.RegularPages "Type" "in" "writing" }} though, does return pages, but not organized as I want it.

Inside of the loop .CurrentSection does show either ‘essays’ or ‘notes’.

I’ve found a solution, however it’s not really clean in my opinion.

{{ $count := 0 }}
{{ range (where .Site.RegularPages "Section" "writing") }}
    {{ if ne $count 5 }}
        {{ if eq .CurrentSection.Title "Essays" }}
            <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
            {{ $count = add $count 1 }}
        {{ end }}
    {{ end }}
{{ end }}

If you want 5 essays followed by 5 notes:

{{ $p1 := first 5 (where site.RegularPages "CurrentSection.Title" "Essays").ByDate.Reverse }}
{{ $p2 := first 5 (where site.RegularPages "CurrentSection.Title" "Notes").ByDate.Reverse }}
{{ range union $p1 $p2 }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}

If you want to mix them together, then sort:

{{ $p1 := first 5 (where site.RegularPages "CurrentSection.Title" "Essays") }}
{{ $p2 := first 5 (where site.RegularPages "CurrentSection.Title" "Notes") }}
{{ range (union $p1 $p2).ByDate.Reverse }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}
1 Like