Create taxonomy terms page with pages meeting certain page parameter conditions


#1

I use the following code to make a taxonomy terms page for ‘tag’:

{{ partial "header.html" . }}
<body lang="en">
{{ partial "subheader.html" . }}

<section id="main">
  <div>
   <h1 id="title">{{ .Title }}</h1>
   {{ $data := .Data }}
    {{ range $key,$value := $data.Terms.ByCount }}
    <h2><a href="{{ $data.Plural }}/{{ $value.Name | urlize }}"> {{ $value.Name }} </a> {{ $value.Count }} </h2>
    {{ end }}
  </div>
</section>

<aside id="meta"> </aside>

{{ partial "footer.html" . }}

But I want to create the terms counts only using documents that have a given value of a Page parameter (hide != true). This doesn’t work:

{{ partial "header.html" . }}
<body lang="en">
{{ partial "subheader.html" . }}

<section id="main">
  <div>
   <h1 id="title">{{ .Title }}</h1>
   {{ $data := where .Data.Pages ".Params.hide" "!=" true }}
    {{ range $key,$value := $data.Terms.ByCount }}
    <h2><a href="{{ $data.Plural }}/{{ $value.Name | urlize }}"> {{ $value.Name }} </a> {{ $value.Count }} </h2>
    {{ end }}
  </div>
</section>

<aside id="meta"> </aside>

{{ partial "footer.html" . }}

#2

You can add custom parameters to your taxonomy terms by creating a corresponding _index.md file. You can learn more in the docs

Then you can access those parameters in your templates.


#3

@guayom I don’t think this will help. I’m not trying to filter taxonomy terms, but to filter pages that make up taxonomy term counts.

I’m trying to restrict the .Data.Pages to certain pages that have a page-level parameter value. Basically, I have a ‘hide’ parameter set to ‘true’ on several pages that I don’t want to make publicly available. I use where statements in my section partials to filter out these pages. But these pages are still being counted in my taxonomy terms page for tags. For instance, I could have a page with the ‘go’ tag that I have hidden, but this page is still counted.


#4

$data is a set of pages. And $data.Terms is the taxonomy itself in each page. I’m not 100% sure that I understand what you’re trying to do, but it looks like you are really trying to range through $data and not $data.Terms. If you try:

 {{ range $key,$value := $data }}

Do you get what you need but just in a different order?


#5

@guayom Still not what I’m looking for, but this has helped me clarify my question. Starting a new topic.


#6

Update: The above clarified for me that .Count accesses a pre-generated count of term occurrences; this suggests what I want to do (count the term occurrences for certain pages only) is getting away from the point of a static site (where the counts are generated once up front.) I’ve decided to move the files I want to hide/keep out of term counts into a different directory than the content folder, and move them back in when I want. Not the most convenient solution, but best for now.


#7

Actually, using a parameter, like you were proposing, it’s a very good idea. It’s just that I don’t know exactly what you are trying to do. But you can use parameters to filter, organize and count your pages. That’s why frontmatter is so useful. Maybe explain a little more in detail your use case, and we will try to help.


#8

I want a page that lists all ‘tags’ that are used on my site. The tags should be sorted by count, and the count should appear next to the tag. I should be able to link from the tag to a separate page showing all the pages associated with that tag. BUT I only want pages that don’t have the page parameter ‘hide’ set to false to be used to create the counts.

Presently, the code I use to create the taxonomy terms page appears below. But pages that are hidden are still counted. So, I may have seven pages with the tag ‘go’, but on three pages have the parameter ‘hide: true’; the tag count that appears is 7, even though there is no way to view these hidden pages.

This file is saved as /layouts/taxonomy/tag.terms.html

{{ partial "header.html" . }}
<body lang="en">
{{ partial "subheader.html" . }}

<section id="main">
  <div>
   <h1 id="title">{{ .Title }}</h1>
   {{ $data := .Data }}
    {{ range $key,$value := $data.Terms.ByCount }}
    <h2><a href="{{ $data.Plural }}/{{ $value.Name | urlize }}"> {{ $value.Name }} </a> {{ $value.Count }} </h2>
    {{ end }}
  </div>
</section>

<aside id="meta"> </aside>

{{ partial "footer.html" . }}

#9

Ok, got it now.
First of all, have you tried using the param “draft” insted of hide? Does that work for you?

Now, here’s a way to display the count of pages using the hide param. But I haven’t been able to sort them by count.

<ul>
    {{ range $key, $taxonomy := .Site.Taxonomies.tags }}
    {{ $pages := where $taxonomy.Pages ".Params.hide" "!=" true }}
      <li><a href="/tags/{{ urlize $key }}">{{ title $key }} ({{ len $pages }})</a></li>
    {{ end }}
</ul>

#10

@guayom Thanks. I hadn’t thought of using the draft parameter (because what I’m doing isn’t suppressing drafts as much as hiding finished course content until I’m ready)–but using draft definitely accomplishes what I’m trying to do.


#11

Great. Those are good news. I was testing for a while and the only way I found to do it with a custom param instead of using draft, is by using scratch. So it’s doable, but not as simple.