How to paginate categories in Jekyll with Github Pages?
Asked Answered
H

1

10

New to Jekyll converting a WordPress blog I'm trying to add pagination to my category layout. In _layouts directory I've created a file named category.html. I can successfully render a particular category with:

category.html:

---
layout: default
---
{% assign catName = page.title | string %}
{% for post in site.categories[catName] %}
    <p>{{ post.title }}</p>
{% endfor %}

When I try to paginate a category's posts after following Jekyll's Pagination documentation:

{% for foobar in paginator.posts %}
    <p>{{ foobar.title }}</p>
{% endfor %}

the code renders nothing. Upon my research Github Pages limits what plugins can be used and I cannot verify if jekyll-paginate-v2 is allowed.

My file structure:

_config.yml:

plugins:
  - jekyll-feed
  ## - jekyll-paginate-v2
  - jekyll-paginate

exclude:
  - Gemfile
  - Gemfile.lock

collections:
  category:
    output: true

defaults:
    scope:
      path: ""
      type: category
    values:
      layout: "category"

paginate: 1
paginate_path: "/page:num/"

Gemfile:

gem "github-pages", group: :jekyll_plugins

group :jekyll_plugins do
  gem "jekyll-feed", "~> 0.6"
  ## gem "jekyll-paginate-v2", "~> 1.7"
  gem "jekyll-paginate"
end

Research:

For a Jekyll site on Github Page how can I create pagination for the category to render only posts for that category? This would be the equivelent to WordPress' category.php.

Heck answered 9/5, 2019 at 18:2 Comment(0)
S
13

Paginate only paginates all posts and not by category or tag.

Paginate V2 does this even for collections. But you cannot run this plugin on Github pages (allowed plugins).

Two solutions :

  1. publish your code in a branch, generate your site locally (or with a service like Travis Continuous Integration that is free for open source projects) and publish (or let your CI publish) your generated code in another branch.

  2. use a modern hosting provider like Netlify that allows you to use any plugin.

Slemmer answered 9/5, 2019 at 20:45 Comment(2)
Just now coming back to this thanks for the answer. To clarify I'm understanding this correctly I could get pagination with Paginate V2 but only if I store it on a server outside of Github Pages. The only that needs to be loaded to a server is the contents of the directory site?Infralapsarian
I wouldn't think of it as "store it on a server outside of Github Pages." You can still host static files on Github Pages, you just can't use Github Pages to build the site with most plugins. Do the build on your local computer and push the contents of _site to your gh-pages branch.Dennis

© 2022 - 2025 — McMap. All rights reserved.