I’m building an archive page, wherein I have three criteria:
- Paginate the pages
- Exclude the ten newest posts [as these are already on a separate ‘blog’ page].
- Group the remaining posts under Date headings.
This works to paginate AND exclude the ten newest posts:
{{ $paginator := .Paginate (where .Data.Pages "Type" "post") }}
{{ range after 10 $paginator.Pages }}
//show posts
{{end}}
This works to paginate AND group the posts under Date headings:
{{ $paginator := .Paginate (where .Data.Pages "Type" "post") }}
{{ range $paginator.Pages.GroupByDate "2006 Jan" }}
<h3>{{ .Key }}</h3>
{{ range .Pages }}
//show posts
{{end}}
{{end}}
However, I’m having difficulties achieveing all three at once. The closest I’ve got is this:
{{ $paginator := .Paginate (where .Data.Pages "Type" "post") }}
{{ range $paginator.Pages.GroupByDate "2006 Jan" }}
<h3>{{ .Key }}</h3>
{{ range after 10 .Pages }}
//show posts
{{end}}
{{end}}
Which works, up to a point, but still shows the GroupByDate
headings for the ten posts which have been excluded using after
. In other words, I still get the date headings with nothing underneath, where those posts would have been, if I’d not filtered them out.
If I try to combine the after
and GroupByDate
directives in one:
{{ $paginator := .Paginate (where .Data.Pages "Type" "post") }}
{{ range after 10 $paginator.Pages.GroupByDate "2006 Jan" }}
<h3>{{ .Key }}</h3>
{{ range .Pages }}
//show posts
{{end}}
{{end}}
Hugo doesn’t throw any errors, but none of the posts are displayed, at all.
And, finally, just on the off-chance I could pre-filter the newest ten posts at the paginator stage, I tried moving the after
directive out to the $paginator
call itself. But again, while it didn’t throw up any errors, it resulted in no posts at all being displayed:
{{ $paginator := .Paginate after 10 (where .Data.Pages "Type" "post") }}
{{ range $paginator.Pages.GroupByDate "2006 Jan" }}
<h3>{{ .Key }}</h3>
{{ range .Pages }}
//show posts
{{end}}
{{end}}
I can kludge something together from my “kind of” working version to first check for the existence of a qualifying post before rendering each GroupByDate
header. But I was just wondering if there was a more elegant way to do what I want.
Can anyone put me out of my misery?