Dynamically count posts per section always = 0

Hi,

New Hugo user here :slight_smile:

I have a partial for a sidebar where I try to list all sections with the number of posts in each section. I can’t get the number of post for each section, it is always 0. In this test site I have 10 posts. On the third line after “Categories” it is just a test counting manually a section to be sure this partial is processed.

Here is the partial:

{{ `<!-- layouts/partials/sections-tree.html -->` | safeHTML }}
                <div class="widget widget--categories">
                  <h3 class="h6">Categories. {{ len (where .Site.RegularPages "Section" "==" "blog") }}</h3>
                  {{ $home := .Site.Home }}
                  <div>
                    <ul>{{ template "section-tree-nav" $home }}</ul>
                  </div>
                  {{ define "section-tree-nav" }}
                  {{ $currentPage := . }}
                  {{ range .Sections.Reverse}}
                  {{ $posts := (where .Site.RegularPages "Section" "==" .CurrentSection) }}
                  {{ $postCount := len $posts }}
                  <li>
                      <li>{{ if eq .CurrentSection $currentPage.Section }}<b>{{ end }}<a href="{{ .RelPermalink}}" title="">{{ .Title }}</a> ({{ $postCount }}){{ if eq .CurrentSection $currentPage.Section }}</b>{{ end }}
                      <ul>{{ template "section-tree-nav" . }}</ul>
                    </li>
                  </li>
                </div>
{{ end }}
{{ end }}

and the output:

I am at a lost here

.CurrentSection is an value of the actual page and not of a section!

1st step to try

{{ range site.Sections.Reverse}}{{ $p := .}}
{{ $posts := (where .Site.RegularPages "Section" "==" $p.Section) }}

$p take the “Section” object and separates page object

HTH

Thanks, but still len=0 for each section.

I tryed a short shortcode version

<div>
  {{ range site.Sections.Reverse}}
  {{ $posts := (where .Site.RegularPages "Section" .Section) }}
  {{.Section}}: {{ len $posts }}<br/>
  {{end}}
</div>

works for me

Start simple as you can and extend later - if it works

4 Likes

Hi,

I finally found why it was not working and this is mind blowing for me. I often copy/paste files to keep a stable previous situation. In my layout folder I have “sections-tree.html” and “sections-tree.html.BAK” files.
For whatever reason, the two files interfere when Hugo builds. If i delete the BAK files, I get the post counts for each section. If I add it again I get 0. But this is “sections-tree.html” that is processed because I have unique content not present in BAK file.
I would not saw this if I had not made a mistake and modified the BAK file producing a build error.

Now I already searched a way to add BAK files to some ignore list in Hugo but this seems not possible outside of /content/

Set the BAK to the ignore list …

Configure Hugo | Hugo (gohugo.io)

ignoreFiles = ['\.BAK$', '\.bak$']

Unfortunately this seeting work only for content and data.

one last…

How do you call this template?

{{ partial "sections-tree" . }} or {{ partial "sections-tree.html" . }}

Well :

  • baseof.html → {{ block “main” . }}{{ end }}
  • index.html → {{ define “main” }} … {{ partial “sidebar-home.html” . }} …{{ end }}
  • sidebar-home.html → {{ partial “sections-tree.html” . }}

Create an issue on GitHub.
Should the developers deal with this error.

Good luck

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