Ability to get section names programmatically

See: Ability to disable section list page routes but have valid routes for pages in that section · Issue #13185 · gohugoio/hugo · GitHub

In your example,

{{ range (.GetPage "/docs/core-concepts").Pages }}
  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}

You hardcoded docs/core-concepts in your snippet.

I have a weight attached to docs/core concepts and docs/getting started. I will have many more “topics” for my documentation that will get added on from time to time. Ideally I don’t want to touch the hardcoded programming.

  1. How can I programmatically get the sections under “docs” in the order based on the weight. (i.e. via an iteration)?
  2. For each loop above, how can I get the pages in order (based on their weight)?
1 Like

Read the documentation for build options, specifically the options for list.

If you use list = 'local' the page will be available to local page collections (i.e., the page collection methods on a Page object). The page will not be available to page collections on a Site object.

{{ with site.GetPage "/docs" }}
  {{ range .Sections }}
    <h2>{{ .Title }}</h2>
    {{ range .Pages }}
      <h3><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h3>
    {{ end }}
  {{ end }}
{{ end }}

Also:
https://gohugo.io/getting-started/glossary/#default-sort-order

1 Like

So you are implying I have to extract the “section” from .RelPermalink using a few string functions?

There seems to be no way to get the “section” name apart from the topmost section as you mentioned in a prior post.

i.e. .Section always returns docs

{{ with site.GetPage "/docs" }}
  {{ range .Sections.ByWeight }}
    {{ range .Pages.ByWeight }}
         section:     {{ .Section }}
        title: {{ .Title }} 
        <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h3>
        <br />
    {{ end }}
  {{ end }}
{{ end }}
1 Like

No, and I have no idea how you reached that conclusion. Is this your first time to build a site with Hugo?

I suggest you pull changes from the repository I provided earlier, or clone it again:

git clone --single-branch -b hugo-github-issue-13185 https://github.com/jmooring/hugo-testing hugo-github-issue-13185
cd hugo-github-issue-13185
rm -rf public/ && hugo && tree public
hugo server
1 Like

Thanks.

My confusion was caused by me forgetting you could give a title to the _index.md file for the invisible section list page.

I thought the “section name” came from the directory name.

1 Like

You should set the front matter title field for every content page.

1 Like

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