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

  • 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/

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?


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?


1 Like

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/, 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?

Let me try to ask this another way.

/cats/breeds/ defines some variables like:
weight = 2
title = "Cat Breeds"

When /cats/breeds/ 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.

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/ 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 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 file, suddenly .CurrentSection returns the nested section as I wanted, instead of the top-level section. So this is resolved now. Thanks.

1 Like

Here is how I am currently getting things rendered only in nested sections and not main sections.

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

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 :wink:

1 Like