[Split from https://discourse.gohugo.io/t/list-all-pages-in-sub-folder/28063]
I have one query
{{ range (readDir "content/blog/") }}
{{ if .IsDir }}
<p>{{ .Name | title }}</p>
In the above code we are reading a directory can we filter the directory listing by weight? Since itβs a βSectionβ and it has _index.md file.
Thank you
Please provide more detail:
- Content structure
- Desired result
sure will do
folder structure -
|- content
|- documentation
|- Configuration
|- _index.md
|- file.md
|- Getting Started
|- _index.md
|- fileA.md
my code -
{{ range (readDir "content/documentation/") }}
{{ if .IsDir }}
<p>{{ .Name | title }}</p>
<ul>
{{ $path := printf "documentation/%s/" .Name }}
{{ range where (where $.Site.RegularPages "Type" "config") "File.Dir" $path }}
{{ $active := eq $.RelPermalink (.URL | relLangURL) }}
{{ $active = or $active (eq $.Section (lower .Name)) }}
<li><a {{ if $active }} class="active"{{ end }} href="{{ .RelPermalink }}">{{ .Title }}</a></li>
{{ end }}
</ul>
{{ end }}
{{ end }}
and using this code Iβm trying to get all sections and inner files in a list view.
Now when I range through sections, it displays alphabetically. Can we range it by weight ??
I hope I clearly explained my query.
With this structure:
content/
βββ documentation/
β βββ configuration/
β β βββ conf-a.md
β β βββ conf-b.md
β β βββ _index.md
β βββ getting-started/
β β βββ gs-a.md
β β βββ gs-b.md
β β βββ _index.md
β βββ _index.md
βββ _index.md
I would do this:
{{ range (site.GetPage "documentation").Sections }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
<ul>
{{ range .Pages}}
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
{{ end }}
</ul>
{{ end }}
The default sort order is:
- Weight (asc)
- Date (desc)
- LinkTitle (asc)
- FilePath (asc)
Notes:
- The template code above is not recursive. It will only list the immediate children of the documentation section.
- When you assign weight, use positive or negative numbers, but not zero.
1 Like
works perfectly!! I think i complicated it.
Thank you so much @jmooring
system
Closed
6
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.