Add category index pages with and without pagination
This plugin adds category index pages with and without pagination.
Benefits are:
Assign one or more categories in the YAML front matter of each page:
categories: [Category Pages Plugin, jekyll]
Generate the site and the category pages are generated:
_site/category/
├── category-pages-plugin/
│ └── index.html
├── 好的主意/
│ └── index.html
└── jekyll/
├── index.html
├── page2.html
└── page3.html
In this example there are three paginated index pages for the jekyll
category (apparently, there are many posts for this category), a
single index page for the 好的主意
category and another single index
page for the Category Pages Plugin
category.
Note that the YAML categories
entry should always use brackets []
to make it explicit that it is an array!
You can find this example in the example
directory of the
git repository.
The example
directory contains a basic example project that
demonstrates the different use cases. In order to run Jekyll on these
examples use:
bundle install
bundle exec rake example
The result is put in example/_site
.
Installation is straightforward (like other plugins):
Gemfile
and configuration file andjekyll-paginate
gem (the latter is a requiredgroup :jekyll_plugins do
gem "jekyll-paginate"
gem "jekyll-category-pages"
end
_config.yml
:plugins:
- jekyll-category-pages
_config.yml
which excludes categories from file URLs (they arepermalink: /:year/:month/:day/:title:output_ext
categories
tags on each blog post YAML frontThe following options can be set in the Jekyll configuration file
_config.yml
:
category_path
: Root directory for category index pages. Defaultscategory
if unset.jekyll
atexample/_site/category/jekyll/index.html
.category_layout
: Basic category index template. Defaults tocategory_index.html
. The layout must reside in the standard_layouts
directory.example/_layouts/category_index.html
.paginate
: (Maximum) number of posts on each category indexpaginate
is set to 2.The template for a category index page must be placed in the site’s
_layouts
directory. The attribute category
indicates the current
category for which the page is generated. The page title also defaults
to the current category. The other attributes are similar to the
default Jekyll pagination plugin.
If no pagination is enabled the following attributes are available:
Attribute | Description |
---|---|
category |
Current page category |
posts |
List of posts in current category |
total_posts |
Total number of posts in current category |
If pagination is enabled (i.e., if setting site.paginate
globally in
_config.yml
) then a paginator
attribute is available which returns
an object with the following attributes:
Attribute | Description |
---|---|
page |
Current page number |
per_page |
Number of posts per page |
posts |
List of posts on the current page |
total_posts |
Total number of posts in current category |
total_pages |
Total number of pagination pages for the current category |
previous_page |
Page number of the previous pagination page, or nil if no previous page exists |
previous_page_path |
Path of previous pagination page, or '' if no previous page exists |
next_page |
Page number of the next pagination page, or nil if no subsequent page exists |
next_page_path |
Path of next pagination page, or '' if no subsequent page exists |
An example can be found in example/_layouts/category_index.html
which demonstrates the various attributes and their use.
A category overview with a full listing of all categories can be
created as follows:
<ul>
{% for category in site.categories %}
<li><a href="{{ site.url }}/category/{{ category | first | slugify }}/index.html">{{ category | first }}</a></li>
{% endfor %}
</ul>
Note that the category page paths are URL-encoded when generated by
this plugin. Thus, you have to use slugify
when linking to each
category. This saves you from problems with spaces or other special
characters in category names.
An example listing can be found in example/index.html
which
shows a full listing of categories with corresponding links.
Listing the categories in a single page is particularly simple since
the categories listed in the YAML front matter are directly available
as strings in page.categories
. However, unlike the site-wide
category list in site.categories
the content of page.categories
are just strings and can thus be added as follows (with references):
<ul>
{% for category in page.categories %}
<li><a href="{{ site.url }}/path-to-category-index/{{ category | slugify }}/index.html">{{ category }}</a></li>
{% endfor %}
</ul>
This project contains a Rakefile
that supports the following
tasks:
build
: Runs all tests and builds the resulting gem file.test
: Run all tests.example
: Run Jekyll for the example project.clean
: Clean up all transient files.To run all test cases use:
bundle exec rake test
The tests run different Jekyll configurations and produce output files
that can be read by Ruby. These are then evaluted and validated using
Ruby RSpec.
To build the gem use:
bundle exec rake build
The result is put in the current directory after all tests have been
run.
The following issues and limitations are known:
&
or <
) in permalink paths. Because of that you cannot usepermalink
path as shown above – it must not containThe gem is available as open source under the terms of the MIT
License.