Length of subsection

This is my content structure. I want to add 4 counter on home page, each will show how many *.md files in one subsections

content/
  section/
     subsection1/
       _index.md
       postA.md
       postB.md
     subsection2/
       _index.md
       postC.md
       postD.md
     subsection3/
       _index.md
       postE.md
       postF.md
     subsection4/
       _index.md
       postG.md
       postH.md

I tried the fllowing code

{{ $subsection1 := (where .Site.RegularPages "Section" "==" "subsection1") }}
{{ $subsection1_count := len $subsection1}}

But subsection1_count return 0, Is there some way work? Thanks!

Quote:

.Section

The section this content belongs to. Note: For nested sections, this is the first path element in the directory, for example, /blog/funny/mypost/ => blog .

use .CurrentSection

You mean that this?

{{ $subsection1 := (where .Site.RegularPages "CurrentSection" "==" "section/subsection1") }}
{{ $subsection1_count := len $subsection1}}

The return value is still 0 on home page! Is there another way?

PS: Each sub-section has own specific template, the _index.md file adjust the type in front matter

you missed the dot!
.CurrentSection

{{ $subsection1 := (where .Site.RegularPages ".CurrentSection" "==" "section/subsection1") }}

I think .CurrentSection is “subsection1” etc

try it
range over .Site.RegularPages and print out .Title .Section .CurrentSection

Can you explain it? Hugo Framework, I only know a little.

You can .GetPage the subsection, then get the len of its pages:

{{ $sub1 := .Site.GetPage "section/subsection1" }}
{{ $sub1len := len $sub1.Pages }}

Oh~
It works!
I appreciate your helping me, @pointyfar, @ju52. Thank you very much! Having a nice day!

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