From 90f908e7b0ce0dbb6ab69dae44bf9612237bfef6 Mon Sep 17 00:00:00 2001 From: Luke Francl Date: Sat, 9 Nov 2019 21:58:38 -0800 Subject: [PATCH] Support Go time format strings in permalinks In the vein of an ancient TODO about supporting custom date formatting with strftime, this allows `:`-prefixed Go time format strings in permalink segments. This allows users to customize date-based permalinks any way they need to. For example, with a date of 2019-11-09, the permalink `/:06/:1/:2` will render as `/19/11/9`. See: https://github.com/gohugoio/hugo/commit/07978e4a4922bc21c230fee65052232b829bd1ab#diff-0688a3b65c7f5d01aa216f8d9b57fd00R111-R112 https://discourse.gohugo.io/t/implementing-additional-date-formats-for-permalinks/17860 https://github.com/gohugoio/hugo/pull/6488 --- content/en/content-management/urls.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/content/en/content-management/urls.md b/content/en/content-management/urls.md index 9cb16f52d..14971af43 100644 --- a/content/en/content-management/urls.md +++ b/content/en/content-management/urls.md @@ -38,6 +38,13 @@ permalinks: Only the content under `posts/` will have the new URL structure. For example, the file `content/posts/sample-entry.md` with `date: 2017-02-27T19:20:00-05:00` in its front matter will render to `public/2017/02/sample-entry/index.html` at build time and therefore be reachable at `https://example.com/2017/02/sample-entry/`. +If the standard date-based permalink configuration does not meet your needs, you can also format URL segments using [Go time formatting directives](https://golang.org/pkg/time/#Time.Format). For example, a URL structure with two digit years and month and day digits without zero padding can be accomplished with: + +{{< code-toggle file="config" copy="false" >}} +permalinks: + posts: /:06/:1/:2/:title/ +{{< /code-toggle >}} + You can also configure permalinks of taxonomies with the same syntax, by using the plural form of the taxonomy instead of the section. You will probably only want to use the configuration values `:slug` or `:title`. ### Permalink Configuration Values @@ -80,6 +87,8 @@ The following is a list of values that can be used in a `permalink` definition i `:filename` : the content's filename (without extension) +Additionally, a Go time format string prefixed with `:` may be used. + ## Aliases Aliases can be used to create redirects to your page from other URLs.