# Create a structured content listing (all pages recursively by section)

**URL:** <https://discourse.gohugo.io/t/create-a-structured-content-listing-all-pages-recursively-by-section/31711>\
**Category:** support\
**Created:** [March 12, 2021, 1:26am UTC](https://discourse.gohugo.io/t/create-a-structured-content-listing-all-pages-recursively-by-section/31711 "2021-03-12T01:26:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kernelc](https://avatars.discourse-cdn.com/v4/letter/k/2bfe46/32.png) [@kernelc](https://discourse.gohugo.io/u/kernelc)\
**Post date:** [March 12, 2021, 1:26am UTC](https://discourse.gohugo.io/t/create-a-structured-content-listing-all-pages-recursively-by-section/31711/1 "2021-03-12T01:26:34Z")

</div>

I have structure in hugo:

```auto
content
├── 1.md
├── a
│ ├── 2.md
│ └── 3.md
└── b
    └── 4.md

```

and I want menu list like:

- 1
- a
  - 2
  - 3

- b
  - 4

I’m using now

```auto
<ul>
    {{ range .Site.RegularPages }}
        <li>
            <a href="{{ .Permalink }}">{{ .Name }}</a>
        </li>
    {{ end }}
</ul>

```

which gives me just:

- 1
- 2
- 3
- 4

> I added
> 
> ```auto
> ---
> title: Aaaa
> ---
> 
> ```
> 
> to `_index.md` in `a` section

---

<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:** [March 12, 2021, 5:10am UTC](https://discourse.gohugo.io/t/create-a-structured-content-listing-all-pages-recursively-by-section/31711/2 "2021-03-12T05:10:56Z")

</div>

structure

```auto
content
├── a
│ ├── 2.md
│ ├── 3.md
│ └── _index.md
├── b
│ ├── c
│ │ ├── 5.md
│ │ ├── 6.md
│ │ └── _index.md
│ ├── 4.md
│ └── _index.md
├── 1.md
└── _index.md

```

layouts/partials/walk.html

```auto
<ul>
  {{ range .Pages.ByTitle }}
    <li>
      <a href="{{ .RelPermalink }}">{{ .Title }}</a>
    </li>
    {{ if .Pages }}
      {{ partial "walk.html" . }}
    {{ end }}
  {{ end }}
</ul>

```

layouts/index.html (assumes you have a layouts/\_default/baseof.html)

```auto
{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ .Content }}
  {{ partial "walk.html" . }}
{{ end }}

```

Note that this excludes the home page.

---

<div class="post-metadata">

**Author:** ![kernelc](https://avatars.discourse-cdn.com/v4/letter/k/2bfe46/32.png) [@kernelc](https://discourse.gohugo.io/u/kernelc)\
**Post date:** [March 12, 2021, 12:23pm UTC](https://discourse.gohugo.io/t/create-a-structured-content-listing-all-pages-recursively-by-section/31711/3 "2021-03-12T12:23:00Z")

</div>

Thank you! Awesome code. also I want to ask, is there way to list sections first and disable creation of list pages?

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [March 12, 2021, 1:49pm UTC](https://discourse.gohugo.io/t/create-a-structured-content-listing-all-pages-recursively-by-section/31711/4 "2021-03-12T13:49:28Z")

</div>

See:

> **[Build Options](https://gohugo.io/content-management/build-options/#readout)**
>
> Build options help define how Hugo must treat a given page when building the site.

---

<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:** [March 12, 2021, 3:27pm UTC](https://discourse.gohugo.io/t/create-a-structured-content-listing-all-pages-recursively-by-section/31711/5 "2021-03-12T15:27:39Z")

</div>

> [@kernelc](#):
>
> is there way to list sections first and disable creation of list pages

I don’t understand your question. Please provide an example of what you wish to achieve.

---

<div class="post-metadata">

**Author:** ![kernelc](https://avatars.discourse-cdn.com/v4/letter/k/2bfe46/32.png) [@kernelc](https://discourse.gohugo.io/u/kernelc)\
**Post date:** [March 12, 2021, 7:28pm UTC](https://discourse.gohugo.io/t/create-a-structured-content-listing-all-pages-recursively-by-section/31711/6 "2021-03-12T19:28:58Z")

</div>

Sorry for my english. (in menu) I want first render sections which has subpages and then standalone pages (sort in another way than .ByTitle). Also I want to prevent rendering list pages (\_index.md) at all. Thanks for help again.

---

<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:** [March 12, 2021, 9:10pm UTC](https://discourse.gohugo.io/t/create-a-structured-content-listing-all-pages-recursively-by-section/31711/7 "2021-03-12T21:10:49Z")

</div>

Sorry, but I still don’t understand what the final result should look like.

The code above produces this:

![image](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/2X/8/816aad62068472232e7b5cfcf3ed7ce730903183.png)

What do you want? Show me.

---

<div class="post-metadata">

**Author:** ![ddct](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@ddct](https://discourse.gohugo.io/u/ddct)\
**Post date:** [March 13, 2021, 11:26am UTC](https://discourse.gohugo.io/t/create-a-structured-content-listing-all-pages-recursively-by-section/31711/8 "2021-03-13T11:26:09Z")

</div>

> [@kernelc](#):
>
> I want first render sections which has subpages and then standalone pages (sort in another way than .ByTitle)

I haven’t tested this yet but the idea is step-by-step exactly like what you want:

```auto
<ul>
  {{ range .Pages.ByTitle }}
    {{ if .Pages }}
      <li>
        <a href="{{ .RelPermalink }}">{{ .Title }}</a>
      </li>
      {{ partial "walk.html" . }}
    {{ end }}
  {{ end }}
  {{ range .Pages.ByTitle }}
    {{ if not .Pages }}
      <li>
        <a href="{{ .RelPermalink }}">{{ .Title }}</a>
      </li>
    {{ end }}
  {{ end }}
</ul>

```

> [@kernelc](#):
>
> Also I want to prevent rendering list pages (\_index.md) at all

onedrawingperday’s answer is for this:

> [@Create a structured content listing (all pages recursively by section)](https://discourse.gohugo.io/t/create-a-structured-content-listing-all-pages-recursively-by-section/31711/4):
>
> See:

---

<div class="post-metadata">

**Author:** ![kernelc](https://avatars.discourse-cdn.com/v4/letter/k/2bfe46/32.png) [@kernelc](https://discourse.gohugo.io/u/kernelc)\
**Post date:** [March 13, 2021, 1:15pm UTC](https://discourse.gohugo.io/t/create-a-structured-content-listing-all-pages-recursively-by-section/31711/9 "2021-03-13T13:15:22Z")

</div>

This is exactly what I was looking for. Thank you all.

---

<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:** [March 15, 2021, 1:15pm UTC](https://discourse.gohugo.io/t/create-a-structured-content-listing-all-pages-recursively-by-section/31711/10 "2021-03-15T13:15:36Z")

</div>

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