I’m trying to group content by year, but Hugo is putting them all into one group with a key name of “0001”. Here’s the code I’m using
{{ range $.Site.Recent.GroupByPublishDate "2006" "rev" }}
{{ .Key }}
{{ end }}
I’m trying to group content by year, but Hugo is putting them all into one group with a key name of “0001”. Here’s the code I’m using
{{ range $.Site.Recent.GroupByPublishDate "2006" "rev" }}
{{ .Key }}
{{ end }}
See the docs.
You are mixing incompatible examples:
{{ range .Data.Pages.GroupByPublishDate "2006-01" }}
Also note that .Recent
is depcrecated and replaced by .Pages
- and will be removed in the next release (if we remember to do so).
Thanks for the help! Here what I came up with.
{{ $groups := $.Site.Pages.GroupByDate "2006-01" }}
{{ range $index, $group := $groups }}
{{ $key := first 4 $group.Key }}
{{ $prevKey := and (ne $index 0) (first 4 (index $groups 0).Key) }}
{{ if (and $key (and (ne $key $prevKey))) }}
{{ $key }}
{{ end }}
{{ end }}
Is this the best way to group by year? By taking the first 4 characters of the year-month string, then removing duplicates.
UPDATE
It turns out GroupByDate "2006"
works fine. It’s the publish date one that’s off. I think this is because I haven’t set the data for it and it defaults to “0001” as the year? No clue what the difference is though or how to set it.
The date you are seeing is likely Go’s zero value.
Set date in front matter for each post:
date: 2015-03-03T16:27:49+01:00