Custom template for one category?

I’m puzzled about taxonomy templates. I’ve read Template lookup order | Hugo I want to use a custom .html template for a category called “blog”, but not for any other categories. I’m not using any _index.md files for front matter for categories.

In config.toml, I have

[taxonomies]
tag = “tags”
category = “categories”
author = “authors”

I have a taxonomy.html file at /layouts/_default/taxonomy.html

In /layouts/categories/ I have a blog.htm file, but /layouts/_default/taxonomy.html is being used for all categories.

But, if I add a list.html or a taxonomy.html template to /layouts/categories/, either one will be used for all categories.

How do I use one specific template for the “blog” category and not all other categories?

2 Likes

You can specify a custom layout in categories/blog/_index.md

layout: blogcategorylayout

then have the corresponding layout file in one of the following places:

categories / blogcategorylayout . html
taxonomy   / blogcategorylayout . html
category   / blogcategorylayout . html
_default   / blogcategorylayout . html
2 Likes

Thanks, using layout = "blog" in /content/categories/blog/_index.md and a file in /layouts/categories/blog/blog.html works.

One more question: how do I set a single.html template for blog single posts? /layouts/categories/blog/single.html doesn’t work. And I don’t see a lookup order for a single posts in a taxonomy here: https://gohugo.io/templates/lookup-order/

That’s because there isn’t really such a thing as a taxonomy “single” page.

What does your site structure look like? Do you have your repo somewhere we can have a look at? It may be easier to help you solve your issue if you describe “what” you are trying to accomplish instead of “how” you are trying to do it. The typical scenario for example, is to have a contents/blog/ section instead of setting categories: blog.

Thanks, I think that’s a better idea. I want a content type like a post, but called “blog” that has posts and a different single.html layout than my other regular posts.

So I have a folder and _index.md
/content/blog/_index.md
with

+++
title = “Blog”
type = “blog”
+++

and then for permalinks in config.toml I have

[permalinks]
posts = “/:year/:month/:title/”
blog = “/:year/:month/:title/”

And for content, I assume I need folders like this?

/content/blog/2020/04/blog-post-name.md

In layouts, what do I need? A folder called blog ? With a list.html and a single.html?

Option 1

blog = /:year/:month/:title/"

With permalinks for blog configured like you have above, you can do the following:

/content/blog/blog-post-name.md

If your content file has date in the frontmatter, Hugo will know to render at yoursite.com/2020/04/blog-post-name/.


Option 2

If you organise your content like:

/content/blog/2020/04/blog-post-name.md

Then you don’t need the permalinks config for blog; Hugo will render your page at yoursite.com/blog/2020/04/blog-post-name.


There are pros and cons to each.

With the first option, you can just dump everything under content/blog/ and Hugo renders the pages at the appropriate year and month subdir: yoursite.com/year/month/title/. However yoursite.com/year/ (and /month/) will not have a list page, ie there will not be a yoursite.com/2020/ with all the 2020-dated posts listed. There are ways around this, but it does not happen ‘automatically’ like the /blog/ section list page would.

With the second option, you have to maintain the year/month organisation in your content yourself, ie you have to create a content/blog/year/month folder yourself for each month for which you have posts. The upside to this is that Hugo is able to generate list pages for you in a pretty straightforward way. (just make sure there is a _index.md file for the sub-folders (ie the year and month sub-folders).


How you organise things is up to you and your preferences.


As for which layout files you need:

  • layouts/blog/list.html
  • layouts/blog/single.html

should do the trick.

I have some more complicated examples in this post I wrote. But I don’t know if that is just information overload at this point :sweat_smile:


1 Like

Thanks! That makes sense. I went with #2, as I want to make content easier to manage in year/month folders. But I don’t care about lists for each year and month, so I don’t need the _index.md files in each. And I didn’t realize I didn’t need permalinks in toml if I had my own directory structure.

The list and single files work in /layouts/blog/.

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