Search code examples
yamlrubygemsjekyllliquidpermalinks

Jekyll: set permalink based on parameter in potst's front matter


I have simple jekyll site. Every post in _posts has paramter author in it's front matter:

---
layout: post
title: my title
author: John Doe
---

...

How should I set permalink in _confing.yml if i want it to be /author/title/?

For example post above would have permalink:

mysite.com/john-doe/my-title/

Thank you for help.


Solution

  • I solved this using jekyll_custom_permalink plugin.

    Installation

    Add the plugin to your site's Gemfile:

    group :jekyll_plugins do
      # your other jekyll plugins...
      gem 'jekyll_custom_permalink', '~> 0.0'
    end
    

    Then run

    $ bundle install
    

    Usage

    Lets assume that you have some options for your collection "projects" defined in _config:

    collections:
      projects:
        output: true
        permalink: projects/:title/
    

    You would like to use custom Front Matter in the paths of your projects. For example, it would be great to be able to include the Front Matter variable type in the links to your projects, which is defined in every file belonging to the "projects" collection.

    ---
    layout: page
    type: python
    title: MyAwesomeProject
    ---
    Some content
    

    You can use the type variable in your permalink by first adding it to an array of custom permalink placeholders for the collection, and then adding the placeholder to the permalink setting prefixed with a : like every other Jekyll placeholder.

    collections:
      projects:
        output: true
        custom_permalink_placeholders: ["type"]
        permalink: projects/:type/:title/
    

    These settings lead to the "project" page, shown above, to be live at /projects/python/MyAwesomeProject.

    SOURCE: https://github.com/NiklasEi/jekyll_custom_permalink/blob/master/README.md