Checking for Hugo Paginated Pages (site wide)

After seeing the YOAST SEO in Hugo earlier today I thought how it’s really not practical to have 3 different <head> partials for index.html, list.html and single.html

There are already plenty of tips in this forum about doing various checks and customize meta tags, titles etc based on the page context from within a single <head> partial.

But the one thing that had been kind of hard -at least for me- was how-to-do a check for Hugo Paginated Pages site wide.

Not anymore.
Here’s how I did it:

{{- $siteTitle := .Site.Title -}}
{{ if ne .Kind "page" }}
{{ $pag := .Paginate (where .Data.Pages "Section" "blog") }}
    <title>{{ .Site.Title }} {{ if $pag.HasPrev }}• Page {{ .Paginator.PageNumber }} of {{ .Paginator.TotalPages }}{{ end }}</title>
{{- else if .IsPage -}}
    <title>{{ $title }} • {{ $siteTitle }}</title>
{{- end -}}

Now Paginated Pages after the first one on the Home Page and on my Blog Section will have a SEO friendly Page of X Pages in the title tag.

And for reference’s sake here is how I invoke Hugo’s paginator in index.html

  {{ $pag := .Paginate (where .Data.Pages "Section" "blog") }}
  {{ range $pag.Pages }}
    {{ .Render "li" }}
  {{ end }}

And in list.html

{{ range .Paginator.Pages }}
{{ .Render "li" }}
{{ end }}
5 Likes