I’ve been trying out the Typo theme and I’ve been trying to get the posts list on the homepage/section pages grouped by year, but I’m unable to figure this out, possibly because of the .Paginate
functions. I’d kinda merged all of this into the range definition for the posts lists on the theme I was using earlier. could you suggest how I could go about this?
this is the relevant section of the template. I’m just not clear on where to add the page groups function here.
<div class="list-container">
{{ with .Site.Params.homeCollectionTitle}}
<h1> {{ . }} </h1>
{{ end }}
{{ $pages := where .Site.RegularPages "Section" .Site.Params.homeCollection
}}
{{ $paginationSize := 1}}
{{ if (gt .Site.Params.paginationSize 0) }}
{{ $paginationSize = .Site.Params.paginationSize }}
{{ end }}
{{ $paginator := .Paginate $pages $paginationSize }}
{{ range $index, $page := $paginator.Pages }}
{{ partial "post-entry.html" $page}}
{{ end }}
{{ partial "pagination-controls.html" $paginator}}
{{ end }}
</div>
I’m not sure exactly what’s happening with your layout file, but here is my archive shortcode. It GROUPS by year and then ranges through them:
{{ range (collections.Where .Site.RegularPages "Section" "posts").GroupByDate "January 2006" "asc" }}
{{ $.Scratch.Set "current" false }}
{{ range collections.First 1 .Pages }}
{{ if compare.Eq (.Date.Format "2006") ($.Get 0) }}
{{ $.Scratch.Set "current" true }}
{{ end }}
{{ end }}
{{ if compare.Eq ($.Scratch.Get "current") (true) }}
{{ range collections.First 1 .Pages }}
<strong>
{{ lang.Translate (fmt.Printf "month_long_%d" .Date.Month) }}
{{ .Date.Format "2006" }}
</strong>
{{ end }}
<ul class="list-inline">
{{ range .Pages }}
<li>
{{ .Date.Format "2" }}.
{{ lang.Translate (fmt.Printf "month_long_%d" .Date.Month) }}
{{ .Date.Format "2006" }} - <a href="{{ .Permalink }}">{{ .Title }}</a>
</li>
{{ end }}
</ul>
{{ end }}
{{ end }}
It uses $.Scratch.Set
so it is VERY OLD, but it’s still working on one of my websites (that is from the early v0.64 hugo times).
Basically what you want to do is “ranging through grouped pages”. Which in GoHugo language is this: {{ range (collections.Where .Site.RegularPages "Section" "posts").GroupByDate "January 2006" "asc" }}
edit to add:
If you want to do all pages (that are not list pages), run this:
{{ range (collections.Where .Site.RegularPages).GroupByDate "January 2006" "asc" }}
My example above lists the years of pages in the posts
section.
yeah, it’s got a bit of a weird thing going and I guess that’s why I’ve not been able to figure this out myself. I’m currently using a considerably simpler template myself
<section class="posts">
{{- range (.Paginate ((where .Site.RegularPages "Type" "in" "blog").GroupByDate "2006")).PageGroups }}
<h3> {{ .Key }} </h3>
<ul id="posts">
{{ range .Pages }}
<li>
<a href="{{ .Permalink }}"> {{ .Title }}</a>
<small><time>{{ .Date | time.Format (i18n "posts.date") }}</time></small>
</li>
{{ end }}
</ul>
{{- end }}
</section>
i guess i wanted to see if I could make it work with the existing layout on this new theme I’m trying. I’m at a point where I think I’ll just rewrite it myself and get it done with ¯\_(ツ)_/¯
You can override any theme file with your own files by just adding them to your layouts
folder. Anything in your root directory overrides the theme files.
I’m aware! I’m considering just writing my own template for the homepage and throwing that section out the window, haha. thanks!