6
votes

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.

1

1 Answers

6
votes

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.