Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to paginate categories in Jekyll with Github Pages?

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:

  • Upgrade Jekyll 3 - jekyll-paginate
  • Pagination
  • jekyll-paginate-v2
  • Generate category page in jekyll targeting github pages
  • Jekyll paginate

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.

like image 264
DᴀʀᴛʜVᴀᴅᴇʀ Avatar asked May 09 '19 18:05

DᴀʀᴛʜVᴀᴅᴇʀ


People also ask

Does GitHub use Jekyll?

GitHub Pages are public web pages for users, organizations, and repositories, that are freely hosted on GitHub's github.io domain or on a custom domain name of your choice. GitHub Pages are powered by Jekyll behind the scenes, so they're a great way to host your Jekyll-powered website for free.


1 Answers

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.

like image 133
David Jacquel Avatar answered Oct 02 '22 04:10

David Jacquel