Listing pages that belong to a given category but within a section only

Hi all,
Apologies, this may be a very trivial question as I’m totally new to Hugo.

I have a section called “notes” with a landing page (“section.html”) displaying the categories of all pages within it, as follows:

Categories:
<ul class="posts">
{{ range $taxonomyname, $taxonomy := .Site.Taxonomies }}
  {{ if eq "categories" $taxonomyname }}
    {{ range  $key, $value := $taxonomy }}
      {{ if ge (where $value.Pages "Section" "notes") 1 }}
      <div class="col-md-3 col-sm-6 col-xs-12">
        <a href="/notes/custom_page_with_list_of_pages_in_this_section_with_category_key/{{ $key | urlize }}">{{ $key | humanize }} </a>
      </div>
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}

As you can see, this code enumerates the categories for pages within it and creates a link for each one of them ("/notes/custom_page_with_list_of_pages_in_this_section_with_category_key…").

I would like each of these links to point to a list of all pages holding the linked category within this section. So that /notes/custom_page_with_list_of_pages_in_this_section_with_category_key/my_category lists all pages with category my_category under section notes . I don’t know in what file location would I place code so that Hugo loads it from a custom URL as exemplified above.

P.S.: This is similar to what Hugo categories taxonomy already does by default at /categories/my_category … but I want it for my “notes” section only.

Similar question: Taxonomies per section - #4 by Reddog"

Thanks in advance.

TLDR

I recommend trying Method 2 first.

When compared to Method 1:

  • Method 2 has fewer moving parts
  • Method 2 is less fragile
  • Method 2 is less error-prone
  • Method 2 takes less time to setup

Method 1 - Roll Your Own

A. You must create two custom templates:

layouts/
└── _default/
    ├── baseof.html
    ├── home.html
    ├── list.html
    ├── single.html
    ├── taxonomy-custom.html  <-- Terms in section within given taxonomy
    └── term-custom.html      <-- Pages in section within given term

B. You must create a section for each taxonomy within each content type, and you must create a content page for each term within each taxonomy.

content/notes/
├── categories/
│   ├── category-a.md
│   ├── category-b.md
│   ├── category-c.md
│   └── _index.md
├── note-1.md
├── note-2.md
├── note-3.md
├── note-4.md
└── note-5.md

C. If you want to do the same thing within a top-level “videos” section, you must re-create the taxonomy structure under “content/videos/categories.”

D. Here’s an example:

git clone --single-branch -b hugo-forum-topic-34504 https://github.com/jmooring/hugo-testing hugo-forum-topic-34504
cd hugo-forum-topic-34504
hugo server

Method 2 - Use Existing Taxonomy System

config.toml

[taxonomies]
'notes/category' = 'notes/categories'
'articles/category' = 'articles/categories'

content/notes/note-1.md

+++
title = "Note 1"
date = 2021-08-30T13:33:56-07:00
draft = false
'notes/categories' = ['category-a','category-b']
+++

content/articles/article-1.md

+++
title = "Article 2"
date = 2021-08-30T13:41:36-07:00
draft = false
'articles/categories' = ['category-a','category-b']
+++

Here’s an example:

git clone --single-branch -b hugo-forum-topic-18423 https://github.com/jmooring/hugo-testing hugo-forum-topic-18423
cd hugo-forum-topic-18423
hugo server

At this point everything just works. If you want to override the taxonomy and/or term titles, you will need to so within each content section:

content/notes/
├── categories/
│   ├── category-a/
│   │   └── _index.md
│   ├── category-b/
│   │   └── _index.md
│   ├── category-c/
│   │   └── _index.md
│   └── _index.md
├── note-1.md
├── note-2.md
├── note-3.md
├── note-4.md
└── note-5.md

thank you, this is brilliant! I opted for method 2. I must say that URL path separator you introduced within a taxonomy key (e.g. ‘notes/categories’ ) fills me with artistic horror, love it :joy:

For the sake of completeness I replaced line 4 with {{ if eq "notes/categories" $taxonomyname }}.

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