Check for Pagination

I create my own content designs under the Layout folder. Then I add content to these folders under the content folder. I add more than one content to a folder and display all the content using pagination.

To make pagination, I need to create a pagination structure for each folder while creating my designs under the Layout folder and I do this with the code below.

    {{ $pages := (.Paginate (where site.RegularPages "Section" "webmaster") ).Pages }}
    {{ $paginator := .Paginate ($pages) }}
    {{ range $paginator.Pages }}
        {{ $title := .Title }}
        {{ $summary := .Summary }}
        <section class="item">
          <div>
            <h1 class="title"><a href='{{ .RelPermalink }}'>{{ $title }}</a></h1>
          </div>
          <div class="summary">
            <p>{{ $summary }}</p>
          </div>
        </section>
    {{ end }}

    {{ if or ($paginator.HasPrev) ($paginator.HasNext) }}
        <div class="pagination">
          {{ if $paginator.HasPrev }}
              <span class="prev">
                  <a href="{{.Paginator.Prev.URL}}">
                      <span class="arrow">ā†</span>
                  </a>
            </span>
          {{ end }} 
          {{ if $paginator.HasNext }}
              <span class="next">
                  <a href="{{.Paginator.Next.URL}}">
                      <span class="arrow">ā†’</span>
                  </a>
              </span>
          {{ end }}
        </div>
    {{ end }}

I have explained so far for informational purposes, I do not have any problem with what I have described above.

But I want to add these tags in the SEO section if there is pagination. If there is no pagination, these codes should not be added. So I need to check this with the if structure.

<link rel="prev" href="{{ .Paginator.Prev.URL | absURL }}">
<link rel="next" href="{{ .Paginator.Next.URL | absURL }}">

The problem is that I canā€™t check if there is pagination on the page. Unfortunately I couldnā€™t do it because I checked the SEO tags in another file and called them in with partical.

I canā€™t check because the following code changes on each page.

{{ $pages := (.Paginate (where site.RegularPages "Section" "webmaster") ).Pages }}

I canā€™t do it for some reason because this code has changed. I wanted to ask here in case there might be a parameter I donā€™t know etc. I would appreciate it if you help.

If you do not understand my question, if you ask where you do not understand, I will try to explain it in a simpler way

The only way is to call the $paginator in the head and your files in a similar way, using if/else.

For a given Page, itā€™s one of the options above. The .Paginator is static and cannot change once created.

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.

(Remember that function arguments are eagerly evaluated, so a call like $paginator := cond x .Paginator (.Paginate .RegularPagesRecursive) is an example of what you should not do. Use if/else instead to ensure exactly one evaluation.)

1 Like

Do you have a chance to show an example code?

Unfortunately it didnā€™t work. If anyone knows another solution, I would be very grateful if you could help me.

This is a detailed description of the situation:
https://discourse.gohugo.io/t/determine-if-current-page-is-result-of-pagination/37494/4

The trick is to paginate before trying to access the pager number.
https://discourse.gohugo.io/t/control-pagination-and-page-collections-from-baseof-html/37643/8

The second topic includes a detailed example that you can clone and test.

1 Like

Thank you for your solution. I also solved it in a different way, even though it is a bit ridiculous, but it works. Iā€™m leaving the code here in case anyone wants to use it.

{{ if .Paginator }}

{{ $paginationonthehomepage:= true }}

    {{ if and (.IsHome) ($paginationonthehomepage) }}

          {{ if ne .Kind "404" }}

            {{ if ne .Kind "taxonomy" }}

                  {{ if .Paginator.HasPrev -}}
                    <link rel="prev" href="{{ .Paginator.Prev.URL | absURL }}">
                  {{ end -}}

                  {{ if .Paginator.HasNext -}}
                    <link rel="next" href="{{ .Paginator.Next.URL | absURL }}">
                  {{ end -}}

            {{ end }}

          {{ end }}

    {{ else }}

          {{ if .IsHome }}

          {{ else }}

                {{ if ne .Kind "404" }}

                  {{ if ne .Kind "taxonomy" }}

                        {{ if .Paginator.HasPrev -}}
                          <link rel="prev" href="{{ .Paginator.Prev.URL | absURL }}">
                        {{ end -}}

                        {{ if .Paginator.HasNext -}}
                          <link rel="next" href="{{ .Paginator.Next.URL | absURL }}">
                        {{ end -}}

                  {{ end }}

                {{ end }}  

          {{ end }}

    {{ end }}

{{ end }}

This code fulfills my request in the question. you can use it if you want.

If you use it, edit the $paginationonthehomepage value according to yourself. If your home page has pagination, set the value to true or false. You can also pull data from any file (config.toml or params.toml) to make it easier to customize.

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