Use different layout for a specific Term in a Taxonomy

Hi,

My website has several categories like -

  1. Java
  2. Kotlin
  3. Spring

I’ve defined a “/layouts/taxonomy/category.html” layout that is used by all the categories.
Now, I want to have a different layout for one of my category - Kotlin.

How can I achieve this? I tried defining a layout in “/layouts/taxonomy/kotlin.html”, but this layout is not picked up by hugo.

I also tried specifying “layout: kotlin” in the front-matter of “/content/categories/kotlin/_index.md” file but the layout is still not picked up by hugo.

Here is how the categories section look like in the content directory -

content
    categories
        java
            _index.md
        spring
            _index.md
        kotlin
            _index.md

The layouts for taxonomy are -

layouts
    taxonomy
        category.html
        kotlin.html    // I want this layout to be used for Kotlin category

Please help!

Hi - could you please have a read of Requesting Help and provide the info mentioned in there. Thanks, as it will help someone help you.

Sure! I have added all the details in the question now.

You’d get more help if you post a link to the repo, or if you can’t, make a small demo site with the options from your config and the content in question. The reason is there are many components that can override or interfere with templates, so we need to see them all to troubleshoot.

@callicoder after reviewing the docs, I am not sure you can call a template for a specific category. I am sure someone will come along and confirm or deny that shortly. :slight_smile:

In your site structure kotlin is term of taxonomy categories, as I understand Hugo can’t define layout for the one term.

You can define a different layout for /category/koitlin/ with a hack.

In your /_default/list.html define your layout like so:

{{ if eq .URL "/category/koitlin/" }}
.......Layout here......
{{ end }}

I have been berated for this hack in the past (see here) but this the only way that I have found to work in a use case like yours. And since this is for just one taxonomy term I can’t see harm in using it.

2 Likes

@alexandros. Thanks! This hack works for me. I’ve tried another hack which works as well -

Inside /layouts/taxonomy/category.html, I’ve done something like this -

{{if eq .Data.Term "kotlin"}}
   //  -- Kotlin Layout here ---
{{else}}
   // -- Generic category layout here ---
{{end}}
1 Like