Tuning the blog - 4) creating category and tag pages

This looked like it was going to be straightforward, but it took me ages to sort this out. Because of the nature of what Jekyll plugins are allowed in GitHub Pages, it is not possible to create tag and category pages. The solution from Stephan Groß - slightly adapted by me - is to create a dummy file for each tag in the folder /blog/tags with a name like tagname.md with an entry like this example for the tag GitHub-pages:

---
layout: blog_by_tag
title: 'Articles by tag: GitHub Pages'
desc: 'GitHub Pages'
tag: github-pages
permalink: github-pages/
---

Something similar was created for categories in the /blog/categories folder. As can be seen from the above example a file blog_by_tag.html needed to be created in the _layout/ folder. This looks like this:

---
layout: default
---

<header id="post-header">
    <h1 id="post-title">{{ page.desc }}</h1>
    <h4 id="post-subtitle">Articles by tag</h4>
</header>

<div id="post-content">
    {% if site.tags[page.tag] %}
        {% for post in site.tags[page.tag] %}
            {% capture post_year %}{{ post.date | date: '%Y' }}{% endcapture %}
            {% if forloop.first %}
                <h3>{{ post_year }}</h3><div class="list-group">
            {% endif %}
		    
            {% if forloop.first == false %}
                {% assign previous_index = forloop.index0 | minus: 1 %}
                {% capture previous_post_year %}{{ site.tags[page.tag][previous_index].date | date: '%Y' }}{% endcapture %}
                {% if post_year != previous_post_year %}
                    </div><h3>{{ post_year }}</h3><div class="list-group">
                {% endif %}
            {% endif %}
		    
            <a href="{{ post.url }}/" class="list-group-item">
                <h4 class="list-group-item-heading">{{ post.title }}</h4>
            </a>

            {% if forloop.last %}
                </div>
            {% endif %}
        {% endfor %}
    {% else %}
        <p>There are no posts for this tag.</p>
    {% endif %}
</div>

I thought that this would be enough, but it needed one more tweak. A change was neccessary to the file _config.yml inserting this line:

relative_permalinks: true

I’m not entirely happy with this as the Jekyll documentation says that this is deprecated and permalinks should always be absolute rather than relative. However at the moment this is the only way I can get it to work. It obviously needs a tweak to blog_by_tag.html and blog_by_category.html, but I haven’t figured out what yet.

Posted in Blogging with : Jekyll, Categories, Tags

Written on January 6, 2015 at 16:52