Restrict pagination to X pages

I’m working with a blog that has 5,500+ blog entries. Because of that, I disabled pagination since it didn’t make sense to have 500+ pages of pagination. I could never see anyone paging through all 5.5k posts.

I’ve decided I’d like to add a bit of pagination back to the site, but just for the 50 or so entries (5 pages). This was easy on the link side:

{{ if lt .Paginator.PageNumber 5 }} <nav id="page-nav"> {{ if or (.Paginator.HasPrev) (.Paginator.HasNext) }} {{ if .Paginator.HasPrev }} <a class="extend prev" rel="prev" href="{{.Paginator.Prev.URL}}"> « {{with .Site.Data.l10n.pagination.previous}}{{.}}{{end}} </a> {{ end }} {{ if .Paginator.HasNext }} <a class="extend next" rel="next" href="{{.Paginator.Next.URL}}"> {{with .Site.Data.l10n.pagination.next}}{{.}}{{end}} » </a> {{ end }} {{ end }} </nav> {{ end }}

But I noticed when I generated a static version of my site, Hugo generated all 500 pages of pagination. I looked at my pagination code in article_list.html. It was originally this:

{{ $paginator := .Paginate (where .Site.Pages "Type" "post") }}

And I thought - if I could make that filter to the top 50, it may work. So I tried this:

{{ $paginator := .Paginate ((first 50 (where .Site.Pages "Type" "post"))) }}

and while it didn’t throw an error, when I generated my site, it still generated all 500+ pages.

Any ideas?

You have another refereence to .Paginate or .Paginator somewhere earlier in the template chain. It is static, first wins.

Nope. I literally just added the .Paginate above last night while testing, and that’s the only place in the site where it is back in.

Just to be clear - pagination.html has it and article_list.html has it. Nothing more.

It either that or stale pages (delete /public) … I implemented pagination, so I’m a little bit categorical about this.

I nuked all of public and I’m still seeing it. Not sure if this is related, but I also have /post/index.html listing all of my posts (5.5k+) full. I’m not too concerned about that - I can delete it in my build script, but I’m still seeing all the pages.

Want to see the code? :slight_smile:

Sure, but the full repo, not just “snippets”.

There are the only files I modified. First, article_list.html:

Previously it looked like so: https://github.com/cfjedimaster/raymondcamdendotcom/blob/master/layouts/partials/article_list.html

Then pagination.html. Previously it was blank. (As I said, I didn’t want the prev/next links.)

See above.

I’m so sorry - I’m so used to asking folks for snippets and not everything I misread. I haven’t committed the updates - one sec.

Repo is here: https://github.com/cfjedimaster/raymondcamdendotcom

Live site, minus this change of course, is https://www.raymondcamden.com.

Your sites i a little big to test … but there are some fishy items here:

On the list pages you have the paginator partial included, yet it is not used … so you will get lots of paginator pages generated for that. For the index page I don’t see anything wrong, but I couldn’t test it.

Hmm. But the page this is used on ends up listing all the content, which I prefer since it gives a one stop place for everything for a particular tag or category. I’ll try disabling pagination here and see what happens.

That did it! Thank you. I plan on blogging about this and will absolutely credit you! :slight_smile:

1 Like

A post was split to a new topic: Paginate last 10