Having Blog Posts Under a Custom Subdirectory
There are many ways we can customize the path a given blog post is under (which is called a permalink). Let’s see the details.
Without setting anything up, Jekyll will use the built-in date permalink style:
/:categories/:year/:month/:day/:title:output_extSince only posts and drafts have categories and dates, for pages and collections, the above is equivalent to
/:title.htmlJekyll lets us set the permalink format in multiple places, the later ones overriding the former settings:
- we can set it globally, in
_config.yml - if the post is part of a collection, we can override the global setting in the collection configuration, also in
_config.yml - and we can override both in the individual post’s front matter
Categories
As we can see in the default style, the path starts with the categories. We can set categories in two ways:
- in the front matter with the
categoryorcategorieskeys. If we use thecategorykey, whatever we set as its value will be used as a single category, spaces included. With thecategorieskey, the value will be split up by the whitespaces.
category: jekyll # path: /jekyll/...
category: jekyll ruby # path: /jekyll%20ruby/...
categories: jekyll ruby # path: /jekyll/ruby/...- by a post’s file path. If our post has the path
./notes/tech/_posts/2022-02-22-my-post.mdfrom the Jekyll root directory, it will have the categoriesnotesandtechautomatically added to it.
We can also combine the two methods: if our post is at the above path, and also has the categories: jekyll ruby line in the front matter, its final categories will be notes tech jekyll ruby, and its default path will be ./notes/tech/jekyll/ruby/2022-02-22-my-post.html.
Setting a custom path for our blog
If we want to have every post under the /blog/ path, we can either set every post’s category to blog or, if we don’t want to use categories at all, we can set the global permalink style to /blog/:title or /blog/:pretty or any other variant we’d like.
Thanks for reading! If you have any comments, additions, or corrections, feel free to reach me via e-mail.