Hugo: Option to change taxonomies URL

Created on 13 Jun 2015  路  22Comments  路  Source: gohugoio/hugo

As discussed here on the forum, it would be nice to have the ability to change the default URLs generated by Hugo for taxonomies.

Right now, we only have plural form, like http://website.tld/tags/cat. An option could give instead http://website.tld/tag/catfor the same URL.

Maybe a good solution would be to let user decide what he wants in the URL. That way, we could choose the singular instead of plural, but also an other word completely (http://website.tld/pet/cat), or nothing at all (http://website.tld/cat).

EDIT : as described here, there's an easy way to have singular instead of plural. But we could have an option to change entirely the URL.

Enhancement

Most helpful comment

@peterychuang I could hack my way around this with your suggestion :smile:

I wondered if using a string such as blog/tags for plural would work, and it turns out it does!

In the templates I had to change things like {{ with .Params.tags }} to {{ with (index .Params "blog/tags") }}, but I got the urls working as I wanted in the end!

So, to sum up:

In config.toml:

[taxonomies]
    tag = "blog/tags"
    category = "blog/categories"

In the front matter of any markdown post:

"blog/categories" = ["One cat", "another cat"]
"blog/tags" = ["One tag", "Another tag"]
+++

I know the front matter looks a little weird, but no way around that :(

All 22 comments

+1

+1

Great idea; +1!

+1

+1 would be useful for us using non-english languages, I've managed to translate everything except the "Tags" :(

This would also allow us to create taxonomy urls that are inside the same folder as a section:

  • /blog/tags/foo
  • /blog/tags/bar

This is the same URL scheme that Jekyll is using.

It is also discussed in create section taxonomies.

+1
That's about the only thing stopping me from switching from Jekyll to Hugo. Everything else works for me, or at least have some workarounds.

Is this still not possible?

I want to create my main website and blog in the same Hugo site, with the contents of the blog all inside /blog. Therefore I want the tags and categories to be /blog/tags and /blog/categories, not just /tags. Anyone figured a workaround for this?

When you define categories and tags in your config file, you have to declare the single and plural form of it as per the documentation:

taxonomies:
  tag: "tags"
  category: "categories"

Correct me if I'm wrong. I believe the taxonomies urls are formed by taking the plural, so it is possible to change the config into something like this:

taxonomies:
  tag: "hello"

and in the front matter of your page, instead of using "tags", you use "hello" to define the tags:

hello:
- tag1
- tag2

The path to the tag listing will become http://yoursite/hello/tag1

You can probably put any string in place of "tags" or "categories," but it cannot be empty.

@peterychuang I could hack my way around this with your suggestion :smile:

I wondered if using a string such as blog/tags for plural would work, and it turns out it does!

In the templates I had to change things like {{ with .Params.tags }} to {{ with (index .Params "blog/tags") }}, but I got the urls working as I wanted in the end!

So, to sum up:

In config.toml:

[taxonomies]
    tag = "blog/tags"
    category = "blog/categories"

In the front matter of any markdown post:

"blog/categories" = ["One cat", "another cat"]
"blog/tags" = ["One tag", "Another tag"]
+++

I know the front matter looks a little weird, but no way around that :(

@dekked Glad that it works out for you.

I figured out a not-so-pretty way to remove "categories" from the url. The downside of doing so are 1) pagination doesn't work, and 2) the original category pages at /categories/cat1 are still being generated.

Not an ideal solution, but works for me, for now.

+1 Really looking forward to this as well.

@dekked Your solution is almost what I wanted (blog/categories, blog/tags).

But I am unable to figure it out how to display all tags/categories at site level.

Currently I am doing it as follows:

{{ range $name, $taxonomy := .Site.Taxonomies.tags }}
{{ $name | title }}
{{ end }}

How to pass "blog/tags" to .Site.Taxonomies?

One way I found to do it is:

{{ range $taxonomyname, $taxonomy := .Site.Taxonomies }}
{{ if eq $taxonomyname "blog/tags" }}
{{ range $key, $value := $taxonomy }}
{{ $key | title }}
{{ end }}
{{ end }}
{{ end }}

Not sure whether it is optimal way or not, but working.

@sivaprasadreddy sorry for the late response. You can do this:

  {{ range $key, $value := index .Site.Taxonomies "blog/tags" -}}
  ...
  {{ end -}}

Stumbled into this problem as well. When you have your posts in /blog/, but your blog tags are at /tag/foo/ instead of /blog/tag/foo/.

For a given taxonomy list, I'm pretty sure you can set the url in the taxonomy lists` frontmatter to whatever you want, but I guess that is not very practical if you want to change them all.

why not general purpose url rewrite configuration? something like this:

[urlRewrite]
"/categories/bla" = "/bla"
"/categories" = false
"/posts/(.*)" = "/$1"

btw. neither content/categories/_index.md nor content/categories/cat1/_index.md using frontmatter url param. Title is used but not url and not slug.

looks like the switch-statement in https://github.com/spf13/hugo/blob/d6000a208c7687ca3a3efd6961ac941ce325e199/hugolib/page.go#L1413 is the source of this problem. If we remove it, we could define urls to taxonomies/tag in permalinks like

[permalinks]
categories = "/:filename:/"

or am I wrong?

+1 in config.toml I want to

[permalinks]
  post = "/:slug/"
  page = "/:slug/"
  category = "/:slug/"

+1 in config.toml I want to

[permalinks]
  post = "/:slug/"
  page = "/:slug/"
  category = "/:slug/"

Did this work?

btw. neither content/categories/_index.md nor content/categories/cat1/_index.md using frontmatter url param. Title is used but not url and not slug.

looks like the switch-statement in https://github.com/spf13/hugo/blob/d6000a208c7687ca3a3efd6961ac941ce325e199/hugolib/page.go#L1413 is the source of this problem. If we remove it, we could define urls to taxonomies/tag in permalinks like

[permalinks]
categories = "/:filename:/"

or am I wrong?

Did we get a resolution on this? I'm a good enough golang programmer to trust myself to not introduce bugs.

Was this page helpful?
0 / 5 - 0 ratings