Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting multiple categories in jekyll

I have a markdown file as follows:

---
title: My Page
categories:
- first
- second
---

In my _config.yml file, I set the permalink to /:categories/:title.html

So when I generate the site, the permalink ends up being /first/second/title.html, whereas I was hoping Jekyll would create /first/title.html and /second/title.html

Is there a way to do this without custom plugins?

Cheers

like image 933
user3653270 Avatar asked Oct 16 '14 17:10

user3653270


2 Answers

The easiest and to me best way is to define the permalink via frontmatter. This is also great for search engine optimization. First you tell Jekyll via _config.yml how Jekyll should build the links if you forget to set it via frontmatter:

_config.yml

# Build settings
permalink: /:categories/:title/

Define a permalink...

2014-10-17_my_post.md

---
layout: post
title: 'Post with permalink'
permalink: /this-is-the-unique-permalink/
---

My Post
like image 80
Phlow Avatar answered Sep 21 '22 23:09

Phlow


According to these docs, it looks like each Jekyll page can only have one category. categories is kind of a misnomer, because you're really defining a "category hierarchy" - like a file path - so the post really resides in a single (sub)category. In this limited sense, you can't do what you want with vanilla Jekyll.

However, Jekyll will process files just sitting around in any directory that doesn't start with an underscore and it follows symlinks. So, for example, if you make directories for each category and place your page in one of them, you can create symlinks to any number of other "categories".

mkdir first second
touch first/page.md
ln -s ../first/page.md second/
like image 39
Max Avatar answered Sep 21 '22 23:09

Max