Search code examples
jekyllyaml-front-matter

Using front matter title as for url permalink


How can I use my front matter for the url instead of the file name?

I have a collection set up in my _config.yml

collections:
      news:
        output: true

I want to be able to use the page.title as my url, below is my front matter

---
layout: newspost
title:  "Fracture announces exclusive 3 year deal with Drake & Morgan."
date:   2019-01-16
---

currently the url is the file name of the .md file as so:

http://localhost:4000/news/drake_and_morgan.html

I would like the output for the url to be as follows:

http://localhost:4000/news/Fracture-announces-exclusive-3-year-deal with-Drake-&-Morgan. or similar.


Solution

  • You can define a slug in front matter :

    ---
    layout: newspost
    title:  "Fracture announces exclusive 3 year deal with Drake & Morgan."
    date:   2019-01-16
    slug: "fracture-announces-exclusive-3-year-deal-with-drake-and-morgan"
    ---
    

    And define a default permalink for news :

    collections:
      news:
        output: true
        permalink: "/:collection/:slug/"
    

    OR

    You can work a little more on filenames and end-up with a fracture-announces-exclusive-3-year-deal-with-drake-and-morgan.md filename. ;-)