# How do I get the nested section that a page is in?

**URL:** https://discourse.gohugo.io/t/how-do-i-get-the-nested-section-that-a-page-is-in/10993
**Category:** support
**Created:** [March 11, 2018, 6:37pm UTC](https://discourse.gohugo.io/t/how-do-i-get-the-nested-section-that-a-page-is-in/10993 "2018-03-11T18:37:37Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ChrisTucker](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@ChrisTucker](https://discourse.gohugo.io/u/ChrisTucker)
#### Post date: [March 11, 2018, 6:37pm UTC](https://discourse.gohugo.io/t/how-do-i-get-the-nested-section-that-a-page-is-in/10993/1 "2018-03-11T18:37:37Z")

</div>

- I have sections on my site, e.g. /content/cats/ and /content/dogs/.
- I have nested sub-sections on my site, e.g. /content/cats/breeds/.
- I have pages within those nested sub-sections, e.g. /content/cats/breeds/persian.md

In my template, I want to be able to look up the nested section for the current page. How do I do this? I can’t figure it out.

Both `.Section` and `.CurrentSection` seem to return the sections “cats” instead of the section “breeds”. The documentation does say that `.Section` returns the top-most section, so that’s expected, but it doesn’t say it for `.CurrentSection`. What am I supposed to use then?

Thanks.

---

<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 11, 2018, 6:49pm UTC](https://discourse.gohugo.io/t/how-do-i-get-the-nested-section-that-a-page-is-in/10993/2 "2018-03-11T18:49:52Z")

</div>

What are you trying to do with such a check?

Are you trying to render the .Parent URL for a breadcrumbs navigation or is it for something else?

---

<div class="post-metadata">

### Author: ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)
#### Post date: [March 11, 2018, 6:52pm UTC](https://discourse.gohugo.io/t/how-do-i-get-the-nested-section-that-a-page-is-in/10993/3 "2018-03-11T18:52:23Z")

</div>

See [https://gohugo.io/content-management/sections/#section-page-variables-and-methods](https://gohugo.io/content-management/sections/#section-page-variables-and-methods)

---

<div class="post-metadata">

### Author: ![ChrisTucker](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@ChrisTucker](https://discourse.gohugo.io/u/ChrisTucker)
#### Post date: [March 11, 2018, 7:02pm UTC](https://discourse.gohugo.io/t/how-do-i-get-the-nested-section-that-a-page-is-in/10993/4 "2018-03-11T19:02:27Z")

</div>

Thanks for the link, but I’ve looked at that page already. As mentioned, `.Section` and `.CurrentSection` only return the top-most section, not the current nested section. `.Type` and `.Parent` also return the top-most section.

I want to do various things with this information. For example, building my navigation menu (looping over sections and sub-sections), displaying the name of the active sub-section on the page, look up front matter values for the nested section (e.g. params defined in `/cats/breeds/_index.md`), etc.

It doesn’t seem like there’s any way to get the current nested section from the page, which seems very odd to me. I thought Hugo had support for nested sections now?

---

<div class="post-metadata">

### Author: ![ChrisTucker](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@ChrisTucker](https://discourse.gohugo.io/u/ChrisTucker)
#### Post date: [March 11, 2018, 7:21pm UTC](https://discourse.gohugo.io/t/how-do-i-get-the-nested-section-that-a-page-is-in/10993/5 "2018-03-11T19:21:54Z")

</div>

Let me try to ask this another way.

/cats/breeds/\_index.md defines some variables like:  
`weight = 2`  
`title = "Cat Breeds"`

When /cats/breeds/persian.md gets rendered by the template, I want to be able to look up the title of its current nested section (i.e. “Cat Breeds”) and display that somewhere on the page.

When building my site navigation menu, I want to be able to loop over sections (/cats/ and /dogs/, which I can already do easily enough), and then loop over sub-sections (e.g. /cats/breeds/, /cats/caring/, /cats/adoption/) in order as defined by their weight.

When building my site navigation menu, I also want to be able to check if the current page belongs to a given sub-section (in the loop) so that I can add extra CSS classes for the active sub-section.

Is it possible to do any of this in Hugo? I was under the impression that Hugo’s support for nested sections means that subfolders are treated like sections now, in the same way that top-level sections work. But it kind of seems like the “support” is just parsing subfolders for .md files and then lumping them all in the same top-level section. I’m hoping that I’m just misunderstanding something.

---

<div class="post-metadata">

### Author: ![ChrisTucker](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@ChrisTucker](https://discourse.gohugo.io/u/ChrisTucker)
#### Post date: [March 11, 2018, 7:30pm UTC](https://discourse.gohugo.io/t/how-do-i-get-the-nested-section-that-a-page-is-in/10993/6 "2018-03-11T19:30:05Z")

</div>

Ahh. Sorry for the triple post, but I think I’ve figured it out now.

`.CurrentSection` _does_ return the nested section after all, as I expected it to.

The problem was that I hadn’t made the `/cats/breeds/_index.md` file yet, as I was trying to see if Hugo even properly supported this first before going to too much trouble.

It seems like having the `_index.md` file there in the subdirectory makes Hugo see it as a section, whereas not having it makes it see it as a meaningless subfolder? That’s my take on it at least. **I feel like this really needs to be documented better** , as I hadn’t seen anything describing this requirement and absolutely nothing made me think of it as something to try.

After creating the \_index.md file, suddenly `.CurrentSection` returns the nested section as I wanted, instead of the top-level section. So this is resolved now. Thanks.

---

<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 11, 2018, 7:33pm UTC](https://discourse.gohugo.io/t/how-do-i-get-the-nested-section-that-a-page-is-in/10993/7 "2018-03-11T19:33:45Z")

</div>

Here is how I am currently getting things rendered only in nested sections and not main sections.  
[https://discourse.gohugo.io/t/list-pages-of-sub-sub-folders/9436/11?u=onedrawingperday](https://discourse.gohugo.io/t/list-pages-of-sub-sub-folders/9436/11?u=onedrawingperday)

And here is how I am building a breadcrumbs navigation on a single page that renders the link to the parent nested section.

[https://discourse.gohugo.io/t/implementing-breadcrumb-navigation-in-hugo/1048/42?u=onedrawingperday](https://discourse.gohugo.io/t/implementing-breadcrumb-navigation-in-hugo/1048/42?u=onedrawingperday)

Just make sure to replace .URL with .Permalink for the links in the above linked snippet.

I think that you will be able to get what you need done with the snippets in the links I posted.

Also you should be able to access the parameters you need from the nested section `_index.md` 😉
