[SOLVED] Unable to override permalink URL with slug in frontmatter

According to the documentation it should be possible to override the URL with slug or url in frontmatter but it doesn’t seem to work for me. I would like post permalinks like 2017-09 by default, but want to be able to override this on specific posts. Instead I get two posts with the same permalink: 2017-09.

I have observed this happening on at least Hugo v0.27.1 and v0.29. This simple setup can reproduce it:


baseURL = "http://example.org/"
languageCode = "en-us"
title = "My New Hugo Site"
theme = "hyde"

  post = "/:year-:month/"


title: "Test Post 1"
date: 2017-09-27T16:29:39+03:00

Testing a post.


title: "Test Post 2"
date: 2017-09-27T16:31:16+03:00
slug: testing-page

Testing another post.

I’m using slug on different pages and it works (latest Hugo Release). Make sure that slug in Front Matter is not after a list or and array (like [menu.main] or [[products]].

I have the following order:

title       = "Development Resources"
date        = "2017-07-12T12:39:57+02:00"
description = "This description comes from resources.md"
draft       = false
series      = ["Resources"]
categories  = ["Docs"]
tags        = []
bodyclass   = "resources"
slug        = "/useful-resources/"

name   = "Sources"
weight = 35

Hope this helps.

@Leo_Merkel my example here is enough to reproduce the issue—there are no lists or arrays in the frontmatter. Just curious, what do you have as the permalinks setting in your site configuration?

post = "/:year/:month/:title/"
page = "/:slug/"

Thanks, @Leo_Merkel. Is the frontmatter snippet above a top-level page or a post? I’m guessing the difference here is you’re creating top-level pages, not posts. In my example all my content are posts, and no permalink setting makes any difference.

It’s a top level page. But it works as well on posts. I have them in a directory articles and use the following Front Matter for slug (sorry, it’s in German):

title       = "Beitrag 1"
description = "Zusammenfassung des Beitrags 1."
date        = "2016-04-13T22:07:07+02:00"
type        = "post"
slug        = "erster beitrag"

categories  = ["Leos Seite"]
tags        = ["eins", "zwei"]

bodyclass   = "beitrag"

Eye-balling it, it looks like you are specifically telling Hugo to skip the slug parameter. A “slug” is the portion of the URL that refers to a specific piece of content. The defaults make it so it works as expected for most folks, playing with title and working in common permalink patterns.

But yours says to just make a year and a month. If you have :title or :slug somewhere in the permalink, it should generate as expected.

Hmm, that’s not how I understand the documentation regarding URL management. We should be able to override the slug in frontmatter, regardless of whether the site’s configuration uses :slug in the [permalinks] setting.

I may misunderstand your issue. Do you have a use for the slug, aside from the permalink, and it isn’t working properly?

I think you may be thinking of url after all. The slug changes how the :slug shows up in a permalink. It overrides the :title. url in front matter, however, changes the complete URL, relative to the baseurl.

Have you tried adding something like this to a post’s front matter?

url = "test/out/url"

It should make whichever post render at example.com/test/out/url/.

1 Like

I found a place in the docs where permalinks are explained in more detail, maybe we ought to link to it from the URL management doc: https://gohugo.io/content-management/organization/#path-breakdown-in-hugo

That shows where and how url and slug are used in generating a path. :slight_smile:

1 Like

You’re right, @maiki. I was misunderstanding the use of slug in frontmatter and using url does indeed achieve what I want. I was just coming here to say that the Content Organization page of the docs explains this a little better—although the ASCII flowcharts aren’t very readable if you don’t realize the code blocks expand on hover. :stuck_out_tongue_closed_eyes:

Marking as [SOLVED].


Feel free to make changes to the docs! They get better as we figure out the gaps, and folks contribute their knowledge. :slight_smile: