The article archive disappears when adding Paginator to the <head>

Hugo: v0.120.0

Hello everyone! I am making a meta tag for page navigation (first, last, prev, next). When I add the construction:

{{ with ($.Paginator) }}
    {{ $canonical = ((.URL) | absURL) }}
    <link rel="first" href="{{ ((.First.URL) | absURL) }}"/>
    <link rel="last" href="{{ ((.Last.URL) | absURL) }}"/>
    {{ if (.HasPrev) }}
      <link rel="prev" href="{{ ((.Prev.URL) | absURL) }}"/>
    {{ end }}
    {{ if (.HasNext) }}
      <link rel="next" href="{{ ((.Next.URL) | absURL) }}"/>
    {{ end }}
{{ end }}

…the list of articles from the archive disappears. I remove the construction and a list of articles appears. I can’t understand what’s wrong.

Below is the code for generating the canonical url and the code from the archive template.

head.html:

{{ $canonical := (.Permalink) }}
{{ with ($.Paginator) }}
    {{ $canonical = ((.URL) | absURL) }}
    <link rel="first" href="{{ ((.First.URL) | absURL) }}"/>
    <link rel="last" href="{{ ((.Last.URL) | absURL) }}"/>
    {{ if (.HasPrev) }}
      <link rel="prev" href="{{ ((.Prev.URL) | absURL) }}"/>
    {{ end }}
    {{ if (.HasNext) }}
      <link rel="next" href="{{ ((.Next.URL) | absURL) }}"/>
    {{ end }}
{{ end }}
<link rel="canonical" href="{{ ($canonical) }}"/>

archive.html:

{{ define "main" }}

    {{ $posts := (where (site.RegularPages) "Type" "in" (site.Params.sections)) }}

    {{ (partial "breadcrumb" .) }}

    {{ if ($posts) }}
        {{ range (.Paginate ($posts.GroupByDate "2006") 50).PageGroups }}
          <div class="row mb-3">
            <div class="col">
              <article>
                <h2>{{ (.Key) }}</h2>
                <div class="node-body">
                  <ul class="list-unstyled">
                      {{ range (.Pages) }}
                        <li class="d-flex">
                          <div class="me-3"><code>{{ (.Date.UTC.Format "01/02") }}</code></div>
                          <div class="flex-grow-1"><a href="{{ (.RelPermalink) }}">{{ (.Title) }}</a></div>
                        </li>
                      {{ end }}
                  </ul>
                </div>
              </article>
            </div>
          </div>
        {{ end }}
    {{ end }}

    {{ (partial "pagination" .) }}

{{ end }}

There is probably a similar topic, but I’m not sure if the problem is the same: I Need Help with Pagination

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.

Thanks for the answer. Is there no way to bypass this?

Which comes first wins. Yes, you can do what you want to do, but it may not be worth the extra work:

https://discourse.gohugo.io/t/control-pagination-and-page-collections-from-baseof-html/37643/8?u=jmooring

1 Like

Yes, thanks to you, I realized my desire. But, really, a lot of actions need to be done to get this desire. I tried to solve the problem as elegantly as possible, but it just doesn’t work. You should always specify variables in the baseof template.

It is much easier to disable pages from an archive. :sweat_smile:

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