# Ability to get section names programmatically

**URL:** <https://discourse.gohugo.io/t/ability-to-get-section-names-programmatically/52872>\
**Category:** support\
**Tags:** pagination\
**Created:** [December 23, 2024, 4:54am UTC](https://discourse.gohugo.io/t/ability-to-get-section-names-programmatically/52872 "2024-12-23T04:54:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjebs](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pjebs/32/23592_2.png) [@pjebs](https://discourse.gohugo.io/u/pjebs)\
**Post date:** [December 23, 2024, 4:54am UTC](https://discourse.gohugo.io/t/ability-to-get-section-names-programmatically/52872/1 "2024-12-23T04:54:36Z")

</div>

See: [Ability to disable section list page routes but have valid routes for pages in that section · Issue #13185 · gohugoio/hugo · GitHub](https://github.com/gohugoio/hugo/issues/13185#event-15743693451)

In your example,

```auto
{{ 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)?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 23, 2024, 5:02am UTC](https://discourse.gohugo.io/t/ability-to-get-section-names-programmatically/52872/2 "2024-12-23T05:02:36Z")

</div>

Read the documentation for [build options](https://gohugo.io/content-management/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.

```plaintext
{{ 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](https://gohugo.io/getting-started/glossary/#default-sort-order)

---

<div class="post-metadata">

**Author:** ![pjebs](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pjebs/32/23592_2.png) [@pjebs](https://discourse.gohugo.io/u/pjebs)\
**Post date:** [December 23, 2024, 5:22am UTC](https://discourse.gohugo.io/t/ability-to-get-section-names-programmatically/52872/3 "2024-12-23T05:22:11Z")

</div>

> [@jmooring](#):
>
> `<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h3>`

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`

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

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 23, 2024, 5:25am UTC](https://discourse.gohugo.io/t/ability-to-get-section-names-programmatically/52872/4 "2024-12-23T05:25:35Z")

</div>

> [@pjebs](#):
>
> So you are implying

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:

```plaintext
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

```

---

<div class="post-metadata">

**Author:** ![pjebs](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pjebs/32/23592_2.png) [@pjebs](https://discourse.gohugo.io/u/pjebs)\
**Post date:** [December 23, 2024, 5:33am UTC](https://discourse.gohugo.io/t/ability-to-get-section-names-programmatically/52872/5 "2024-12-23T05:33:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 23, 2024, 5:35am UTC](https://discourse.gohugo.io/t/ability-to-get-section-names-programmatically/52872/6 "2024-12-23T05:35:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [December 25, 2024, 5:36am UTC](https://discourse.gohugo.io/t/ability-to-get-section-names-programmatically/52872/7 "2024-12-25T05:36:03Z")

</div>

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