Add a category page in Jekyll
I use categories in my blog posts to collect the posts that belong together, but since Jekyll doesn’t create category pages and Github pages don’t accept plugins I had to create it myself.
As always I had to read a bunch of forum posts and web pages to get some hints and this page was a good help.
Category layout
The first thing you do is to create a new layout for categories and you put it
in _layouts
and maybe you can call it category.html
.
Here is my category layout:
category.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
---
layout: default
---
<h2>Category: {{ page.category }}</h2>
{% for post in site.categories[ page.category ] %}
{% capture this_year %}{{ post.date | date: "%Y" }}{% endcapture %}
{% capture this_month %}{{ post.date | date: "%B" }}{% endcapture %}
{% unless year == this_year %}
{% assign year = this_year %}
<h3>{{ year }}</h3>
{% endunless %}
{% unless month == this_month %}
{% unless forloop.first %} </ul> {% endunless %}
{% assign month = this_month %}
<h4>{{ month }}</h4>
<ul>
{% endunless %}
<li><span class="category_date">{{ post.date | date: "%a %d %b %Y" }}</span>
<a href="{{ post.url }}">{{ post.title }}</a></li>
{% if forloop.last %} </ul> {% endif %}
{% endfor %}
It will use page.category from the calling page to find the posts with that category. It will show headers for year and month and then list the posts of that month.
I really wish all languages had something similar to forloop.first and forloop.last it really helps keeping the code clean. Just a side note.
Next you need category pages for every category you use. That could possibly be tedious if you have a bunch. I have 3 so far and I will try to keep them under 10. But if you speak perl, ruby or python you could go through your post directory and just create category files with a script.
This is my Jekyll-category page:
jekyll.html
1
2
3
4
5
---
layout: category
permalink: /categories/Jekyll/
category: Jekyll
---
And that’s it. It’s sets the layout to category, a permalink and a category. The category parameter is what the category.html layout will use to find the posts. So you will need a similar file for all your categories.
Update
Here I write about the perl script I wrote.