How to Specify Category Slug?

Clearly, this is the best support channel ever – a new version of the software right after a request from a random dude.

@bep any chance you could point to a specific section in the documentation? Otherwise, github commit would be as useful.

We just released it so the docs at http://gohugo.io/ is updated. A relevant search term could be “_index.md”.

1 Like

@bep the 0.18 release looks awesome. Thank you. By using the front matter for taxonomies, is there any way to access it from the taxonomy terms template?

Yes, that is one of the main points about it. Just do {{ .Title }} etc. inside the templates.

@bep not sure we are on the same page, except I’m missing something.

I have

content/
    categories/
        infosec/
            _index.md
layout/
    taxonomy/
        category.html
        category.terms.html

_index.md contains title: Information Security.
category.terms.html contains

    <ul>
    {{ range $key, $value := .Data.Terms }}
      <li><a href="#">{{ $key }}</a></li>
    {{ end }}
   </ul>

In this case $key is infosec, which is correct. It doesn’t look like I can access .Title of infosec/index.md from inside of the range – this results to an error:

ERROR: 2016/12/22 08:59:08 general.go:236: Error while rendering page : template: taxonomy/category.terms.html:9:25: executing "taxonomy/…

You leave me guessing a little with those half-pasted errors … but the key/value you get in that range is a list of terms / wegithed page list pairs.

So, pseudo code, not tested:

 {{ range $key, $value := .Data.Terms }}
      <li><a href="#">{{ $key }}</a></li>
     {{ range $value }}
    {{ .Weight }}:  {{ .Page.Title }}
    {{ end }}
    {{ end }}

Or something like that.

Sorry. You are accessing the front matter of a post that belongs to a term. But what I’m curious about is accessing the front matter of a specific term from the category.terms.html template.

As an example, see this page: http://spf13.com/topics/.
It has an open-source taxonomy (that is capitalized with css). I’m trying to map it to Open Source string. In my case, I have an infosec taxonomy that I want to output as Information Security on the terms list page (/categories) specifically.

P.S. It’s all good if I go to a term page (/categories/infosec/). I can access front matter from there.

Sorry if I sound confusing.

Well, that was the template you showed me, so I assumed that was the problem area.

I suspect that you have a “context issue” – i.e. you try to do a .Title inside the range. Try $.Title. But, I’m guessing a little, as your example missed a piece or two.

Yes, that is exactly what I’m trying to achieve here. Unfortunately, it still doesn’t work as expected.

url: http://localhost:1313/categories/
code (taxonomy/category.terms.html):

<ul>
    {{ range $key, $value := .Data.Terms }}
      <li><a href="#">{{ $.Title }}</a></li>
    {{ end }}
</ul>

output:

Categories
Categories

expected output:

Programming
Information Security

Just to clear up something, the $ variable references the initial context of the current template. That is why $.Title doesn’t change. It’s handy when you want to reference the current page (or whatever) from inside a loop in a given template (since the . context changes within loops).

@moorereason understood. Thank you. Giving this page as an example http://spf13.com/topics/, is there any way to map taxonomy term to a custom string and show this whole list of taxonomies based on custom values? Something like:

/topics/
Development → Web Developemnt
MongoDB → MongoDB Database
Pesonal → Personal Posts

?

@andreyg, you’re blazing a trail on new Hugo features!

See Content for home page and other list pages at the end of https://gohugo.io/overview/source-directory/. I think you want to create content/categories/infosec/_index.md with a title defined in the front-matter.

That’s exactly what I have. I feel like {{ range .Data.Terms }} does not understand the context.

OK, I think I understand.

In Hugo there are

  • Taxonomy terms
  • Taxonomy lists

Both can have a template and content file(s).

So, for the categories example:

/content/categories/_index.md // <= terms
/content/categories/infosec/_index.md // <= list

The template you are editing is the taxonomy terms template. You have no content file for the categories terms, so you get the default (“Categories”).

What you (probably) want to do is to update your list template or whatever template is used to render your taxonomy lists.

But you can also test out the theory by putting an index.md file into the /categories folder and see what happens.

@bep correct. I use

/layouts/_defaults/terms.html

to render all taxonomy terms. (In my previous example I showed it as /layouts/taxonomy/category.terms.html which is the same as /layouts/_defaults/terms.html according to https://gohugo.io/templates/terms/)

Alright, the only way I found to achieve what I want is by using .Site.Pages.

    {{ range where .Site.Pages "Type" "categories" }}
      <li>{{ .Title }}</li>
    {{ end }}

.Data.Terms simply doesn’t know anything about _index.md.

I believe you can also use .Data.Pages (which should give you the pages that is relevant, and more portable)

Nope. .Data.Pages is empty.

That we should fix. Until then, your where clause is a good workaround.

But just to be clear for people hitting this page from search: There is also a Page on the terms level (i.e. categories, tags), that can have front matter etc.

1 Like

https://github.com/spf13/hugo/issues/2826

2 Likes