Grouping and sorting archives by year and month

I’ve been trying to change my archives page to group and sort the posts by year and month, but everything I’ve read and tried so far hasn’t worked. I’m assuming it’s because they’re relying on taxonomies and front matter from standard Hugo that isn’t accessible in Micro.blog.

I’m hoping to have a setup as follows:

2021
     Month1
             - post 1
             - post 2
             - …
     Month2
             - post 1
             - …

Anyone know how I can implement this?

1 Like

you can do something like what I do for reverse chron by day, but chronological within day:

{ define "main" }}  

{{ $posts := where site.RegularPages "Type" "post" }}
{{ $grouped := $posts.GroupByDate "2006-01-02" }}
{{ $paginated := (.Paginate ($grouped)) }}
<div class="content list h-feed">
{{ range $paginated.PageGroups }}
{{ $thedate := (time .Key) }}
<h1 class="day"> {{ $thedate.Format "January 2, 2006" }}</h1>
  {{ range .Pages.Reverse }}
    {{ if eq .Type "post" }}
      {{ partial "post_header.html" . }}
       <div class = "post-content e-content">
         {{ .Content }}
       </div>
     {{ partial "post_footer.html" . }}
    {{- end }}
  {{ end }}
<hr>
{{ end }}
</div>
{{ partial "paginator.html" . }}
{{ end }}

Note the $posts.GroupByDate — that’s the format for daily groups, but you can choose to use monthly or annually. Basically, I’d filter to a year of posts and then group those by month, and then do a range over those groupings. I’m pretty sure you could also group by year, range over those, group by month, range over those inside to do the whole thing as nested for loops but not 100% sure.