Using Pagination With Terms

for my terms.html file I have:

{{ partial "header.html" . }}

<h1>{{ .Title }}</h1>

<ul class="terms">
  {{ range $key, $value := .Data.Terms }}
  <li>
    <a href="{{ (print "/" $.Data.Plural "/" $key) | relURL }}">
      {{ $key }}
    </a>
    ({{ len $value }})
  </li>
  {{ end }}
</ul>

{{ partial "footer.html" . }}

I can’t find any guides which explain how exactly terms work and how I can paginate them. While I am asking I would like to do something like {{if eq .Title “Tags”}} order by most popular {{ else }} order by alphabetical. :stuck_out_tongue: but just figuring out how to be paginated would be super great.

What I want to do is paginate my list of all terms/taxonomies on my site.

.Paginator.Pages will give you pagination for the taxonomies in the current term.

So replace what you’ve got with something ala:

{{ range  .Paginator.Pages }}

{{ .Title }}: {{ .RelPermalink }}
{{ end }}

And add the paginator navigator:

{{ template "_internal/pagination.html" . }}

I have that for my list.html template, listing all posts within a taxonomy, but for my terms.html template I need to list all my taxonomies. I don’t think your code does that.

Have you tested my code?

1 Like

No, I haven’t… and I don’t want to dismiss good advice. It just doesn’t look like any terms template I have seen… and your wording

taxonomies in the current term.

implies that you are giving a list of posts, and not a list of taxonomies. That’s also what the code looks like it’s doing.

No example on this page uses similar formatting to what you have, which is why I just want to make sure we are on the same page before running it https://gohugo.io/templates/taxonomy-templates/

I’m just trying to be helpful. I usually know what I’m doing when I post some advice.

But up to you.

2 Likes

I think running it will answer this question about being on the same page (pun intended :stuck_out_tongue_winking_eye:).

Also, it should give you the pagination I believe you want at yoursite.com/tags

1 Like

This is all fair :slight_smile: I realize I sounded like a jerk, I should have just put together some sample data and ran it with that and checked it out (as generating this with all my pages is a 3+ hour endeavour) but it just looked so different than everything I have ever seen, and because of his wording I was skeptical we were on the same page so I thought I would just ask. I didn’t mean to look a gift horse in the mouth or anything like that, though I know that’s how it looks now :stuck_out_tongue:

So I tried this code, and it didn’t work (I did also try this code on a page that lists a specific taxonomies posts and it did work, so this does list posts instead of taxonomies), I’m sorry if there was a miscommunication, but I am trying to do pagination on the list of terms not the list of posts with those terms.

I need to use pagination on this: https://gohugo.io/templates/taxonomy-templates/#order-taxonomies

No miscommunication, I understand exactly what you want to do. If you could tell me what “did not work” or point me to the failing source, I may be able to help you.

It didn’t “fail”, just now the pages are totally blank.

My code was

{{ partial "header.html" . }}

<h1>{{ .Title }}</h1>

<ul class="terms">
{{ range  .Paginator.Pages }}
<li>
{{ .Title }}: {{ .RelPermalink }}
</li>
{{ end }}
</ul>
{{ partial "pagination" . }}

{{ partial "footer.html" . }}

I by no means mean this as an “I told you so” :slight_smile: I really want to emphasize that I really appreciate you even taking the time to look at this.

Also saying “it didn’t work” is a terrible way of saying something, I constantly am frustrated by my users saying the same thing and giving me nothing to go on.

One final thing:

I tested this and it does work:

{{if eq .Title "Tags"}}
<ul class="terms">
  {{ $data := .Data }}
  {{ range $key, $value := .Data.Taxonomy.Alphabetical }}
  <li><a href="{{ $.Site.LanguagePrefix }}/{{ $data.Plural }}/{{ $value.Name | urlize }}"> {{ $value.Name }} </a> {{ $value.Count }} </li>
  {{ end }}
</ul>
{{ else }}
<ul class="terms">
  {{ $data := .Data }}
  {{ range $key, $value := .Data.Taxonomy.ByCount }}
  <li><a href="{{ $.Site.LanguagePrefix }}/{{ $data.Plural }}/{{ $value.Name | urlize }}"> {{ $value.Name }} </a> {{ $value.Count }} </li>
  {{ end }}
</ul>
{{ end }}

But sadly I can’t fit pagination in anywhere (lol this is probably due to my being a novice and just copying “.Pagination” before everything.

I will just add this:

  • Pagination only work for Pages (i.e. list of Page) or PageGroup (i.e. what you get from GroupBy...) or WeightedPages (i.e. a single taxonomy).
  • Since Hugo 0.2* something (don’t remember which), what you get in .Data.Pages (and this is used by default in the .Paginator) for a terms page is Pages that represents a list of taxonomy terms.

So, assuming you have a relatively updated Hugo version and have put the template in the right place etc., it should just work. If not, it is a bug.