Creating category specific templates

Like the title says, imagine you have a category ‘tea’ and a category ‘coffee’, my goal is to show a totally different design and lists of related content based on the category.

For example, that page will feature it’s own background, maybe slideshows, and links to other sites. Only after this will the actual posts of that category be rendered.

I read the documentation and built the generic category listing in layouts/taxonomy/category.html. It seems that I can’t have something like layouts/taxonomy/category/coffee.html correct?

My workaround idea is to use partials. If the category name matches coffee, it will render partials/category_coffee.html

Am I on the right track or is there an easier way to do this ?

That is correct. I cannot think of a better way than partials at the spur of the moment, but the partial string can be created dynamically.

1 Like

Why not split coffee and tea into completely different content types? Then you can use different section layouts, e.g. how I’ve done in the following for photos vs. writing:

1 Like

I’m new to Hugo and didn’t consider that approach for lack of knowledge. It would work but I plan on having more categories as time goes by.

I’m sticking with the partials approach for now, as all that will change is inside the same div. And thanks for the reference link, it will surely come in handy :slightly_smiling:

@bep thank you for the pointer, here’s the code I am using in case someone has the same goal:

{{ $category := replace (lower .Title)  " " "-"}}
{{ $catpartial := (print "category_" $category ".html") }}
{{ partial $catpartial . }}