Get A List Of Categories Based In Subfolders In Jekyll

Published:
Last modified:

Overview

In Jekyll, posts located in subfolders, automatically get its parents folders as the post categories.

For example, lets consider the following directory structure that will be useful in the rest of the post:

β”œβ”€β”€ about.md
β”œβ”€β”€ _config.yml
β”œβ”€β”€ index.md
β”œβ”€β”€ _posts
β”‚Β Β  └── 2017-03-03-welcome-to-jekyll.markdown
β”œβ”€β”€ theory
β”‚Β Β  β”œβ”€β”€ index.md
β”‚Β Β  β”œβ”€β”€ testing
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ index.md
β”‚Β Β  β”‚Β Β  └── _posts
β”‚Β Β  β”‚Β Β      └── 2016-08-08-when-to-test-or-write-code-first-in-web-development.md
β”‚Β Β  └── web-development
β”‚Β Β      β”œβ”€β”€ index.md
β”‚Β Β      └── _posts
β”‚Β Β          └── 2016-08-17-authorization-versus-authentication-in-web-apps.md
└── web
    β”œβ”€β”€ index.md
    β”œβ”€β”€ _posts
    β”‚Β Β  β”œβ”€β”€ 2016-11-10-automatically-tweet-new-blog-posts-based-in-rss.md
    β”‚Β Β  └── 2016-12-09-colors-palettes-for-web-design.md
    β”œβ”€β”€ seo
    β”‚Β Β  β”œβ”€β”€ index.md
    β”‚Β Β  └── _posts
    β”‚Β Β      └── 2017-01-26-essential-seo-tips-and-techniques-from-trusted-sources.md
    └── servers
        β”œβ”€β”€ apache
        β”‚Β Β  β”œβ”€β”€ index.md
        β”‚Β Β  └── _posts
        β”‚Β Β      └── 2017-01-07-protect-web-directories-with-http-basic-authentication-in-apache-server.md
        └── index.md

The post /web/seo/_posts/2017-01-26-essential-seo-tips-and-techniques-from-trusted-sources.md will be automatically assigned by Jekyll the following categories: ["web", "seo"].

There are other solution to build a list of categories, keeping a data file with the structure, the downside of this approach is that you will always have to keep in sync with your directory structure or they will be left out of the category list.

Developing a solution capable to get the categories from posts full path, makes it possible to detect new categories at each build, without having you to enter them manually.

In this solution we will create an include where you can specify which level of the path you want, so for example if you want the categories at the first position (level 0), then you will get ["web", "theory"].

But if you want the subcategories of web, you will get ["seo", servers].

This way it is very easy to have a layout with this snippet included, and automatically generate the subcategories list in each subcategory index.md page.

Developing process

If we don’t have yet, we create the _includes folder and a subcategories.html file.

In _includes/subcategories.html we will define a categories liquid array that will hold our categories:

{% raw %}
{% assign categories = "" | split: '/'%}
{% endraw %}

Then we iterate through all posts

{% raw %}
{% assign categories = "" | split: '/'%}
{% for page in site.pages %}
{% assign page_cats = page.dir | split:'/' %}
{% if page_cats[1] == site.docs_dir %}
{% assign category = page_cats[2] | strip %}
{% unless category == "" %}
{% unless categories contains category %}
{% assign categories = categories | push: category %}
{% endunless %}
{% endunless %}
{% endif %}
{% endfor %}
{% endraw %}
Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus


How to get a list of categories with Liquid code specifying the depth level when using subfolders in Jekyll

Clutter-free software concepts.
Translations English EspaΓ±ol

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·