Terms.html template showing a specific section only

I am trying to figure out how to have my terms.html page only show content from a specific section. This taxonomy is used by multiple sections, but I only want the /categories/ page to show a specific section.

inside _default/terms.html

I have the following in my terms.html page:

{{ range where .Pages "Section" "projects" }}

{{ end }}

But it gives a blank page…

If I do

{{ range .Pages  }}

{{ end }}

It does show everything from all sections. What am I doing wrong?

I’m doing it like this: where .Site.RegularPages "Type" "in" .Site.Params.mainSections and in my config.toml, I have:

[params]
  mainSections = ["projects"]

Maybe, you can also use it without the config like this:

where .Site.RegularPages "Type" "in" "projects"

I think I had tried that when I had started working on my website and it had not worked or something, so, I stuck with the config way.

That does not work…

It shows all the pages, not the category taxonomy terms.

That’s strange. It should have as that’s what I’m using currently. However, I’m using it in layouts/_default/list.html. Maybe that creates a difference if you want to try?

Or wait till someone more knowledge answers. :wink:

Hi @mhal, Do you have your code in a repo somewhere we can have a look at? Please have a read through Requesting Help to see how to make it easier to get help here.

Given

[taxonomies]
tag = "tags"
# posts/lorem.md
title: Lorem 
tags: 
- foo
- bar
# posts/ipsum.md
title: Ipsum
tags: 
- bar
- baz

Rendering {{ range .Pages }} from _default/terms.html and navigating to /tags/ should give you a list of foo, bar, baz. Their .Section would of course, be tags.

Rendering {{ range .Pages }} from _default/taxonomy.html and navigating to /tags/bar/ should give you a list of lorem, ipsum. Their .Section would then be /posts/.

Thank you for your reply.

So to explain a little better, I have multiple sections throughout the site that are using the same taxonomy. In my config.toml:

[taxonomies]
  category = "categories"

I have multiple sections on the site using categories: project-news, projects, people.

On my _default/terms.html, I only want to have the categories listed from a specific section of the site, not all of them. I have tried the below, but it is a blank page.

{{ range where .Site.RegularPages "Section" "projects" }}
<ul>
    {{ range .Pages }}
         <li>
               <a href="{{ .Permalink }}">{{ .Title }}</a> {{ .Count }}
         </li>
    {{ end }}
</ul>
{{ end }}

I am certain that this range is the problem:

{{ range where .Site.RegularPages "Section" "projects" }}

I can make all the categories show, but they include the other sections I do not want.

How do I only show the categories from one section?

Are you trying to list the .Pages in /projects/ that have categories? Or are you trying to list the categories cat1 cat2 catx?

Do you have your site project in a repo somewhere? It is easier to help if we can see your site context and code, instead of me trying to replicate based on your description.

Thank you for your reply. I do not have this repo anywhere.

I am just trying to list all the categories for /projects/ and want the _default/terms.html page to only show /projects/

I am using the taxo categories for multiple sections.

That is, if you have this content:

- proj1 (cat1,cat2)
- proj2 (cat2)
- proj3 (cat3, cat5)
- post1 (cat1, cat4)
- post2 (cat6)

What list do you want?

- proj1 (cat1,cat2)
- proj2 (cat2)
- proj3 (cat3, cat5)

or

- cat1 
- cat2
- cat3
- cat5
  • cat1
  • cat2
  • cat3
  • cat5

where section is /project/

Something like:

{{/* "layouts/_default/terms.html" */}}

<ul>
    {{/*  list all categories [cat1, cat2, catx]  */}}
    {{ range .Pages }} 
    {{/*  for each category, check if .Pages with project section > 0  */}}
    {{ if gt (len (where .Pages "Section" "project" )) 0}}
    <li><a href="{{.Permalink}}">{{.Title}}</a> {{ .Section }}
        <ul>
            {{/*  list all content .Pages under project  */}}
            {{ range where .Pages "Section" "project" }}
            <li><a href="{{.Permalink}}">{{.Title}}</a> {{ .Section }}</li>
            {{ end }}
        </ul>
    </li>
    {{ end }}
    {{ end }}
</ul>
2 Likes

Amazing. This is exactly what I was looking for. Thank you so much, @pointyfar!!!

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