Adding a new category to a Jekyll site involves modifying the site’s configuration file and updating the front matter of each post that should be included in the new category.
Here are the steps to add a new category:
- Open your site’s configuration file,
_config.yml
, in a text editor. - Add the new category to the defaults section of the configuration file. For example:
defaults:
- scope:
path: ""
type: posts
values:
category: blog
- scope:
path: ""
type: posts
values:
category: news
In this example, two new categories, ‘blog’ and ‘news’, have been added to the defaults
section.
- Save the changes to
_config.yml
. - Open each post that should be included in the new category and add the category to the post’s front matter. For example:
---
layout: post
title: "My Post Title"
category: "news"
---
In this example, the post has been assigned the ‘news’ category.
- Save the changes to each post’s front matter.
- Finally, you can create a new page that displays all posts in the new category by creating a new Markdown file in the
_pages
directory with the following front matter:
---
layout: category
title: <Category Title>
category: <Name of the category>
---
In this example, replace Category Title with the title of the new category and categoryname
with the name of the new category.
Next, add the following code to the bottom of the new page to display all posts in the new category:
html {% for post in site.categories[page.category] %} {{ post.title }} {{ post.excerpt }} {% endfor %}