Get A List Of Categories Based In Subfolders In Jekyll
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 %}
- Multilingual Jekyll Without PluginsMay 8, 2017
- Host a Jekyll Website With Pretty Urls In Amazon S3 and CloudfrontApril 24, 2017
- Get A List Of Categories Based In Subfolders In Jekyll
- 5 Steps To Add Bootstrap 4 To Jekyll The Right WayFebruary 27, 2017
- Automated Deployment Of Jekyll Websites To Github Pages With A Git Push To GithubNovember 8, 2016
- How To Use Bower Scss With JekyllJune 18, 2016
- How to implement breadcrumbs on a Jekyll site with nested categoriesJune 7, 2016
- How To Handle Adsense In A Jekyll Development EnvironmentJune 6, 2016
- How To Prevent Content Displaying In A Jekyll Development EnvironmentJune 6, 2016
Articles
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
·