Why Jekyll Categories or Tags?
Imagine you have a blog where you discuss very different things all together. Many bloggers post their personal experiences along with some professional posts. Curating information is very important to make users browse through your website with ease. What if New York Times had no categories like Politics, Business, Tech etc..? How hard would it be to track what happened to last night’s football game? There should be a Sports category to make readers’ life easy.
One more thing I want to clear right away is that tags are nothing but categories in Jekyll. And if you want your post links to be
/tag/title/ then you can do that in _config.yml file by using this line
- Why Jekyll Categories or Tags?
- How to implement Jekyll Categories?
How to implement Jekyll Categories?
Before implementing Jekyll categories, see if your content can be divided into certain domains. For example on my blog, I have sorted my posts into Jekyll, Web-Desing, Github, etc. You will see the categories at the end of the article. On clicking them, you will be redirected to a tags page where all blog posts are categorized under certain topics. One post may contain more than one category if it is dealing with more than one topic.
Let’s see how we can implement it.
Add Jekyll Categories to Front matter
This is the first step in any method to implement Jekyll Categories or Jekyll Tags. Decide what categories you want. It is better to have fewer categories. As shown in the example below, add
categories: front matter to all the posts according to their content.
Let’s imagine there are 3 posts in your blog. One is personal, one is tech and another is a mixed post. Here you can organize them into two categories - personal and tech. Now, add these categories to all the posts.
Though you can add as many categories as you like, I recommend not to exceed 10. Too much of anything is bad.
Once that is done, create a page to display them.
Create a Category page
This is the page which will be shown when someone clicks on any category. Something like my jekyll tags page. Copy below code and paste it into a new file and name it categories.html.
The page will look like this. It will have all the categories listed out. One article can be listed in many categories. This happens when you use more than one category for a post.
If you want a simple list of all posts inside a certain category then use the below code. A better example for this would be this website.
Display categories on Jekyll posts.
It is better to show the categories to which the current article belongs to. And upon clicking any of the categories, users should land on the page we created in the previous step.
This can be done with the following liquid syntax. Copy this to post layout wherever you want to show the categories.
This is how we can implement categories. If we want tags instead of categories then we should replace category with tag and categories with tags everywhere.
It is good to categorize items if there is a lot of content on varying topics. But if the content is just on one subject then there is no need to categorize it. Using category slug in the URL helps users to figure out what they will encounter on opening the link. Use Jekyll Categories where you think is necessary.
Let me know if this method worked out for you. Leave any suggestions in the comment section.
This article is written by sharath.dt Follow him @webjeda.