# Check for Pagination

**URL:** <https://discourse.gohugo.io/t/check-for-pagination/42124>\
**Category:** support\
**Tags:** pagination\
**Created:** [December 28, 2022, 5:56pm UTC](https://discourse.gohugo.io/t/check-for-pagination/42124 "2022-12-28T17:56:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ardacarofficial](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ardacarofficial/32/21327_2.png) [@ardacarofficial](https://discourse.gohugo.io/u/ardacarofficial)\
**Post date:** [December 28, 2022, 5:56pm UTC](https://discourse.gohugo.io/t/check-for-pagination/42124/1 "2022-12-28T17:56:38Z")

</div>

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.

```auto
    {{ $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.

```auto
<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.

```auto
{{ $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

---

<div class="post-metadata">

**Author:** ![steppa](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@steppa](https://discourse.gohugo.io/u/steppa)\
**Post date:** [December 28, 2022, 6:10pm UTC](https://discourse.gohugo.io/t/check-for-pagination/42124/2 "2022-12-28T18:10:07Z")

</div>

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.)

> **[Pagination](https://gohugo.io/templates/pagination/)**
>
> Hugo supports pagination for your homepage, section pages, and taxonomies.

---

<div class="post-metadata">

**Author:** ![ardacarofficial](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ardacarofficial/32/21327_2.png) [@ardacarofficial](https://discourse.gohugo.io/u/ardacarofficial)\
**Post date:** [December 28, 2022, 6:33pm UTC](https://discourse.gohugo.io/t/check-for-pagination/42124/3 "2022-12-28T18:33:00Z")

</div>

Do you have a chance to show an example code?

---

<div class="post-metadata">

**Author:** ![ardacarofficial](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ardacarofficial/32/21327_2.png) [@ardacarofficial](https://discourse.gohugo.io/u/ardacarofficial)\
**Post date:** [December 29, 2022, 2:42pm UTC](https://discourse.gohugo.io/t/check-for-pagination/42124/5 "2022-12-29T14:42:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 29, 2022, 4:17pm UTC](https://discourse.gohugo.io/t/check-for-pagination/42124/6 "2022-12-29T16:17:55Z")

</div>

This is a detailed description of the situation:  
[https://discourse.gohugo.io/t/determine-if-current-page-is-result-of-pagination/37494/4](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](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.

---

<div class="post-metadata">

**Author:** ![ardacarofficial](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ardacarofficial/32/21327_2.png) [@ardacarofficial](https://discourse.gohugo.io/u/ardacarofficial)\
**Post date:** [December 29, 2022, 5:14pm UTC](https://discourse.gohugo.io/t/check-for-pagination/42124/7 "2022-12-29T17:14:14Z")

</div>

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.

```auto
{{ 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.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [December 31, 2022, 5:14pm UTC](https://discourse.gohugo.io/t/check-for-pagination/42124/8 "2022-12-31T17:14:24Z")

</div>

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