

Jekyll Category and Tag Taxonomies for custom Collection
A work-around provides Category and Tag iterator for custom Collections in Jekyll static blog generator
Overview#
The main purpose of this category / tag iterator is to create an index page, or a taxonomy. Brief review about Collection will be mentioned, but at this point, you should be able to build, host your own site and understand basic functionality of layout, include and post folders.
Quick View of the result
An alternative would be using the liquid plugin jekyll-archives
, although as mentioned earlier these additional / non-native plugin could be not supported depend on build on the server host.
Prerequisite#
- Define a custom Collection
In case you do not have a custom collection already, it can be quickly configured. In _config.xml
, a custom collection of recipe
is defined as below.
collections:
category: recipe
output: true
defaults:
- scope:
path: ""
type: recipe
values:
layout: single
yaml- Usage in article YAML Front Matter
Once the configuration is defined, simply add it in your article. For example, this tutorial has the following tags defined in its Front Matter.
YAML Front Matter is essentially everything inside the divider (3-dashes) at the beginning of your article.
---
categories: [Tutorial, Productivity]
tags: [Jekyll, Taxonomy]
---
yamlCategory Tag Iterator#
It depends on your purpose to place the following code snippet in the correct place, bu typically this would be in file _layout/categories.html
if you want to create an Index / Taxonomy page.
Collect all data from collection#
- Assign variable
thisCollection
for your custom collection data - Apply filter to keep only category
- group_by: Re-group field data by their
category
- join, split: Order field data into an array with
,
delimeter - map: Keep only data belongs to
site.categories
i.e. our interested categories.
- group_by: Re-group field data by their
{% assign thisCollection = site.portfolio %}
{% assign allCats = thisCollection | map: 'categories' | join: ',' | split: ',' | group_by: category %}
liquidArray for unique values#
This stage is not necessary but recommended, if you’re doing indexer, it is best to count duplicated values and later display that Category only once.
{% capture category_array %}
{% for eachCat in allCats %}
{{ eachCat.name }}|
{% endfor %}
{% endcapture %}
liquidIterate all categories#
Now it is essentially a nested for-loop
to match all of categories of the current article (post.categories
) with the one collected earlier from all articles of a collection.
{% for eachCat in allCats %}
{% for post in thisCollection %}
{% if post.categories contains eachCat.name %}
{% assign thisCat = eachCat.name | append: '|' %}
{% if category_array contains thisCat %}
<!-- Apply your code here to display the category list -->
{% endif %}
{% assign category_array = category_array | remove: thisCat %}
{% endif %}
{% endfor %}
{% endfor %}
liquidTroubleshooting (Extra)#
- Custom Plugin Jekyll Hook
Another approach is writing your own plugin with the Jekyll.Hook method inspired from this StackOverflow Discussion ↗.