Get page number

I would like to be able to access the current pagination number in a partial.

Nothing seems to work.

I’ve even tried:

    {{ $paginatorString := (printf "%#v" .Paginator) }}
    {{ $paginatorString }}
    {{ if in $paginatorString "number:1" }}
      First page
    {{ end }}

    {{ $currentPageNumber := (index (split (index (split $paginatorString "number:") 1) ",") 0) }}
    ({{ $currentPageNumber }})

The above works (I had it in my navigation partial), but my page’s posts disappear.

I just need to see if I’m on the first page of pagination.

I suppose I could parse URL to see if “/posts/page/#/” exists.

Is there any good way to get .PageNumber outside of pagination?

Thanks!

See Pagination | Hugo

{{ .Paginator.PageNumber }} should give you what you want, I think.

Thanks for the help!

I thought that would work too… Unfortunately, I get:

render of "page" failed: ".../layouts/_default/baseof.html:22:7": execute of template failed at <partial "partials/header/basic.html" .>: error calling partial: ".../layouts/partials/header/basic.html:5:17": execute of template failed at <.Paginator.PageNumber>: error calling PageNumber: runtime error: invalid memory address or nil pointer dereference render of "page" failed: invalid memory address or nil pointer dereference  failed to render pages: invalid memory address or nil pointer dereference 

Yea, and it’s impossible for me to help you debug that without the source. You should start by checking what you pass into the partial.

I suspect you are trying to the get the pager number before paginating the page collection.

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

1 Like

Thank you @jmooring! I can’t believe I missed this post when doing a search of the forum. :man_facepalming:

Much appreciated!!!

Thank you for the help @bep! Sorry for the lack of details. For the record, I was just passing the context into basic.html from baseof.html: {{ partial "partials/header/basic.html" . }}

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