Pagniation seems to use 'byWeight' even when I don't want it

I know that the default order of content is

Default: Weight > Date > LinkTitle > FilePath

But I want to do it purely by the publish date, on my index.html so I think that .ByPublishDate should be my friend.

But when I use:

  {{- range  (.Paginate .Site.RegularPages.ByPublishDate.Reverse).Pages -}}
    

Note: I’m doing it that way to get all my content types onto the front page. The theme is my own cbp2.

The result seems to be ‘by weight’ and then by publish date. So I think, "aha, I need to order .Pages, so I try:

  {{- range  (.Paginate .Site.RegularPages).Pages.ByPublishDate.Reverse -}}
    

And indeed, that is better, except that the last few items on the first pagination page are there by weight rather than publish date.

You can see this on my site https://petersmith.org/ (the first page is a bit long for some, but the last few pages have published dates in 2011, whereas I should still be on the 2022 content.

My repository is at GitHub - psmith1303/petersmith: My (kinda) commonplace book

I expect I’m doing something blindingly obvious, but I can see it.

BTW, in case there was some odd interaction between the paginator and the ranging, I also tried:

{{- range  (.Paginate .Site.RegularPages.ByPublishDate.Reverse).Pages.ByPublishDate.Reverse -}}

The result was the same as my second bit of code.

You are invoking pagination twice in the same template, so the first one wins.

https://gohugo.io/templates/pagination/#list-paginator-pages

If you call .Paginator or .Paginate multiple times on the same page, you should ensure all the calls are identical. Once either .Paginator or .Paginate is called while generating a page, its result is cached, and any subsequent similar call will reuse the cached result. This means that any such calls which do not match the first one will not behave as written.

Your original range statement is fine.

2 Likes

In my last change, I was too smart for my own good.

I’d read that paragraph many times, but clearly, I didn’t understand what it meant. Thanks for putting me right.

I’ve now sorted my code, and for completeness, here it is.

This didn’t work how I wanted it to :frowning:

{{"<!-- index.html -->" | safeHTML }}

{{- if (eq .Paginator.HasPrev false) -}}
<article class="header">
  <!-- Note that the content for index.html, as a sort of list page, will pull from content/_index.md -->
  {{- .Content -}}
</article>
{{- end -}}

{{- range  (.Paginate .Site.RegularPages).Pages.ByPublishDate.Reverse -}}
    <article class="summary">
      <div class="grid-child">
	<h3><a href="{{ .Permalink }}" class="article-title p-name">{{ .Title }}</a></h3>
	{{ partial "showanyimage" . }}
      </div>
      <div class="grid-child">
	<p>A <a href="/{{ .Type }}"/ alt="Link to all {{ .Type }}">{{ .Type }}</a> entry on {{ .PublishDate.Format "2 Jan 2006" }}</p>
	{{ .Summary }}
	{{- if .Truncated }}
	<p><a href='{{ .Permalink }}'>Full item … </a></p>
	{{- end }}
	{{ partial "category-list" . }}
	{{ partial "tag-list" . }}
      </div>
    </article>
{{- end -}}

But this does work as expected :slight_smile:

{{- $paginator := (.Paginate .Site.RegularPages.ByPublishDate.Reverse) -}}

{{- if (eq .Paginator.HasPrev false) -}}
<article class="header">
  <!-- Note that the content for index.html, as a sort of list page, will pull from content/_index.md -->
  {{- .Content -}}
</article>
{{- end -}}

{{- range  $paginator.Pages -}}
    <article class="summary">
      <div class="grid-child">
	<h3><a href="{{ .Permalink }}" class="article-title p-name">{{ .Title }}</a></h3>
	{{ partial "showanyimage" . }}
      </div>
      <div class="grid-child">
	<p>A <a href="/{{ .Type }}"/ alt="Link to all {{ .Type }}">{{ .Type }}</a> entry on {{ .PublishDate.Format "2 Jan 2006" }}</p>
	{{ .Summary }}
	{{- if .Truncated }}
	<p><a href='{{ .Permalink }}'>Full item … </a></p>
	{{- end }}
	{{ partial "category-list" . }}
	{{ partial "tag-list" . }}
      </div>
    </article>
{{- end -}}

Again, thank you.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.