Bug in order content with weighted and unweighted pages

I want Paginator Pages in `Lastmod’ order in my index page
It is normal at first, but when I post some content(such as “index.md”) to server,
hugo refresh the pages automatically, and the all the weighted pages are the first.
However, when I restart hugo server, everything is back to normal(all in ‘Lastmod’ order)

here’s my “index.html” code:

{{ define "main" }}
    {{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
    {{ $pag := .Paginate ($pages.ByLastmod.Reverse) }}
    <section class="article-list">
        {{ range $index, $element := $pag.Pages }}
            {{ partial "article-list/default" . }}
        {{ end }}
    </section>

    {{- partial "pagination.html" . -}}
    {{- partial "footer/footer" . -}}
{{ end }}

{{ define "right-sidebar" }}
    {{ partial "sidebar/right.html" (dict "Context" . "Scope" "homepage") }}
{{ end }}

@StellarisW I made the same mistake trying to combine sort terms. Instead of

$pages.ByLastmod.Reverse

you need

($pages.ByLastmod).Reverse

Otherwise only .Reverse is used (that is ByLastmod is ignored).

@cshoredaniel
thanks for answering, I tried

{{ $pag:= .Paginate (($pages.ByLastmod).Reverse) }}

but it didn’t work,
and my mistake is not same as yours, I think,
my mistake is that hugo sorted the items in the priority of Weight > Lastmod(weighted items are at the begginng, then lastmod),
but everything is back to normal (in just Lastmod order ) when I restart the server

What does this contain? Perhaps you have multiple paginations on the same page (inadvertently). Hugo can only paginate once per page.

Also, do you have a source code repo we can look at / thwack on? For something like this it’s difficult to debug without the context.

It contains `.Paginator.Pagers’

I modified the code from repohugo-theme-stack,
the index.html is in layouts/index.html

Ok, that is the problem. Try replacing:

{{- partial "pagination.html" . -}}

with

{{- template "_internal/pagination.html" . -}}

See

for more information

Do you mean change it to default pagination.html?
But it have nothing to do with the sorted pages(or .Paginate)

That template just renders the navigation. It doesn’t create the pagination.

If you want to have custom navigation then instead you need to replace:

.Paginator.Pages with $pag.Pages (and pass $pag into your paritial).

The problem is that you can’t use both .Paginator and .Paginate on the same page.

oh, I get it, I reviewed the docs:

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.

so that’s why when I restart the hugo server the pages’s order is normal again?

but I replaced the code with

{{- template "_internal/pagination.html" . -}}

it is the same problem

Yes.

I didn’t think that through enough. You might want to try:

{{- partial "pagination.html" (dict "page"  . "pager" $pag -}} 

and at the top of pagination.html write:

{{ $ctx := . }}
{{ $curPage := .page }}
{{ $pager := .pager }}
{{ with $page }}

at the end of the partial write:

{{ end }}

and, finally, replace .Paginator.Pages with $pag.Pages

HTH.

EDIT: Sorry the editor is doing annoying things and quotes were messed.

I figure out it’s just the cache problem, because I deleted the code

{{- partial "pagination.html" . -}}

It shows the same thing,
Is there any ways to disable the cache function of pagnation?

Can you show what you currently have?

If you still have the {{- template "_internals/pagination.html" . -}} you should remove it, and use the dict method I showed. That uses the paginator that .Paginate created instead of having a second paginator (so you use the cached version, so both spots are using the same pagination).

There is no way to turn off the paginator cache.

Sorry Pager not Pages.

here’s current code:

{{ define "main" }}
    {{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
    {{ $pag := .Paginate (($pages.ByLastmod).Reverse) }}
    <section class="article-list">
        {{ range $index, $element := $pag.Pages }}
            {{ partial "article-list/default" . }}
        {{ end }}
    </section>

    {{- partial "footer/footer" . -}}
{{ end }}

{{ define "right-sidebar" }}
    {{ partial "sidebar/right.html" (dict "Context" . "Scope" "homepage") }}
{{ end }}

If you are still seeing pagination then there is another paginator somewhere in the theme or your site-specific templates.

Also are you editing the theme or using an override? (You need to use an override, not edit the theme).

now I deleted all the code about .Paginator, and leave the .Paginate,
the page still use the previous cache I think

what do mean using an override?

If you are using hugo-theme-stack in your themes/stack directory (for example), you would need to copy themes/stack/layouts/_default/index.html to layouts/_default/index.html and themes/stack/layouts/partials/pagination.html to layouts/partials/pagination.html and edit the layouts/* versions.

That is you copy the layouts you want to modify to your site root (from the theme) and modify them.

Not sure if I’m clear enough, but I hope I am…

I was editing the theme I suppose, buy I can’t find layouts/ from the root directory,
see:
Snipaste_2022-08-17_03-10-03

Oh, if the directories don’t exist, you need to create them.

okay, thanks alot :grinning:, I’ll try it though

1 Like