How to fix this error ( undefined variable "$paginator" )

Hello, i was trying to apply Canonical Pagination Links for Blog Posts and Tags using this

{{ $href := .Permalink }}
{{ with $paginator }}
  {{ if gt .PageNumber 1 }}
    {{ $href = printf "%s%s/%d/" $.Permalink "page" .PageNumber }}
  {{ end }}
{{ end }}
<link rel="canonical" href="{{ $href }}">

but it keeps giving me this error ( undefined variable “$paginator” )

I think you want to look at .Paginator or .Paginate
Pagination | Hugo (gohugo.io)

There is also a .TotalPages method to test if there is more than one page.

1 Like

$paginator is a variable … you have to asign a value first.

ex:

{{ $paginator := $.Paginator }}

2 Likes