Search code examples
paginationhugostatic-site

HUGO How to use pagination inside terms page?


I'm trying to use a pagination inside a terms page (e.g. /layouts/tags/terms.html) because I got many tags.

And I also want to display all my tags sorted by popularity. For the last point, I did:

{{ range .Data.Terms.ByCount}}
...
{{ end }}

And it works fine.

I tried:

1)

{{ range .Data.Terms.ByCount}}
{{ range .Paginator.Pages }}
...
{{ end }}
{{ end }}

But I got "can't evaluate field Paginator in type hugolib.OrderedTaxonomyEntry" ERROR.

2)

{{ range .Paginator.Pages }}
{{ range .Data.Terms.ByCount}}
...
{{ end }}
{{ end }}

I got no error but no posts are displayed, only the pagination is there.

3)

{{ $paginator := .Paginate .Data.Terms.ByCount }}
{{ range $paginator.Pages }}
...

{{ end }}

I got: "error calling Paginate: cannot convert type hugolib.OrderedTaxonomy to Pages" ERROR.

I tried many others things but I couldn't figure out how to do display all my posts by popularity with a pagination in my taxonomy terms.html page.

Do you have more idea ?


Solution

  • I got the answer from somewhere else; it is:

    {{ $topaginate := slice }}
    
    {{ range $k, $v := .Data.Terms.ByCount}}
    {{ $topaginate = $topaginate | append  (site.GetPage (print $.Data.Plural "/" $v.Term )) }}
    {{ end }}
    
    {{ range (.Paginate $topaginate).Pages }}
    {{ . }}
    {{ end }}