HUGO

Separate tag/category page for different Hugo type

Hi

Let’s say I have two different hugo types: blogs and projects
/content/blogs
/content/projects

All contents can have tags.

My question:
How can I have two different category listing page, for blogs and projects.

I know hugo will auto generate pages
if we got to site.com/tags/individual-tag
but it will be for all the contents, not specific for each type

What I want:
site.com/blogs/tag/{hosting} //$hosting could be any tag
site.com/projects/tag/{client} //$client could be any tag

Really confused right now reading the doc,
what should I do to get these page and url

I haven’t used them before, and others will probably have more relevant advice, but I believe that you are looking for taxonomies.

thanks for ur reply, I do talk about taxonomy, but related to when we have multiple content type

It’s still a taxonomy :wink:

You can create multiple taxonomies. For instance project-tags and post-tags. Add these to the frontmatter, then add layouts for them.

The ACTORS and DIRECTORS in the link by @fekete-robert are basically your project and post tags.

In the end it’s up to you not to mix them up if you don’t want to mix them between post types.

Let’s say you have two taxonomies, and three terms within each taxonomy.

Step 1 - Create Section Pages

hugo new blogs/tags/tag-a/_index.md
hugo new blogs/tags/tag-b/_index.md
hugo new blogs/tags/tag-c/_index.md
hugo new blogs/categories/category-a/_index.md
hugo new blogs/categories/category-b/_index.md
hugo new blogs/categories/category-c/_index.md
hugo new projects/tags/tag-a/_index.md
hugo new projects/tags/tag-b/_index.md
hugo new projects/tags/tag-c/_index.md
hugo new projects/categories/category-a/_index.md
hugo new projects/categories/category-b/_index.md
hugo new projects/categories/category-c/_index.md

Step 2 - Specify Layout

Add this to the frontmatter in each of the files above:

layout = "type-taxonomy-term"

Step 3 - Create Layout

layouts/_default/type-taxonomy-term.html

{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ .Content }}

  {{/* Expected URL path is /type/taxonomy/term/ */}}
  {{ $pathParts := split (trim ((urls.Parse .Permalink).Path) "/") "/" }}
  {{ $type := index $pathParts 0 }}
  {{ $taxonomy := index $pathParts 1 }}
  {{ $term := index $pathParts 2 }}

  {{ $termPage := site.GetPage (printf "%s/%s" $taxonomy $term) }}

  {{ range where $termPage.Pages "Type" $type }}
    <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
  {{ end }}

{{ end }}

Step 4 - Test

http://localhost:1313/blogs/tags/tag-a/
http://localhost:1313/projects/categories/category-c/
etc.
1 Like