About categories

Hello.

I’m migrating from Wordpress to Hugo.

So I have some questions about categories.
I searched several times, but I couldn’t solve it, so let me ask you a question.

First, how do I change the category slug?
For example, I would like to be able to access the category “diary” with “d”.

Then is there a way to change the “/categories” in the category URL to something like “/category”?

I would appreciate it if you could let me know.

Best regards.

Yes, that’s possible. All you need to do is create a folder with the category name in your content directory.

So, your example could look like: content/categories/diary/. In that, create a _index.md file and in it, specify the URL like:

---
url: "/d/"
---

If you want it like, /category/d/, you can also use slug instead of url.

That’s possible too.

In your config.toml, specify it like this:

[permalinks]
    categories= "/category/:slug/"

Hello.

Thank you.

I tried the following, but there seems to be a slight problem.

  1. Create “content/categories/diary/_index.md”
---
slug: d
---
  1. Add the following to “config.toml”.
[permalinks]
categories = "/category/:slug/"

Then I generated an HTML file.
However, a folder for each category is created in the “category” folder, and “index.html” is created in the “categories” folder.
How can I make this so that “index.html” is also created in the “category” folder?

Best regards.

I said, the config.toml should have this:

[permalinks]
    categories= "/category/:slug/"

Notice the singular category being used it. That’s what’s defining the slug for categories. The way you have entered, that is:

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

it’s anyways the default behaviour. and need not be declared.

Hello.

Oh, sorry.
It seems wrong when I copied it to the post.

Best regards.

So does it still not work when you use ‘category’ in config?

Hello.

No, I changed it in the settings and it works.
However, I am in trouble because the structure of the folder is as follows.

categories/index.html
category/d/

I want to change this to the following folder structure.

category/index.html
category/d/

Is this possible?

Best regards.

I think, you can create another file here: content/categories/_index.md and set the frontmatter:

---
url: "/category/"
---

This should do it. However, it should have worked by just using the config. Nevertheless, the method mentioned now would mostly fix it all.

Hello.

The method you taught me worked.
Thank you very much.

Best regards.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.