[SOLVED - solution posted] How do I put .Site.Menu specific to a section in page template

I have content, let’s call i introduction.md that uses the page template. This content is in section childcare. I’d like to list out all items in that childcare section in the page template using .Site.Menu, so while the user is on introduction/ user can see the list of the other pages associated to the section. I want to use the .Site.Menu because I can indicate where the user is in relative to the other pages.

Another way to describe this, go to https://gohugo.io/extras/aliases/ and see the side menu on the left. Aliases is part of Extras. How can I only display Extras and its children in the menu, but not everything else?

How do I go about doing that?

Thank you.
-Z

Nevermind, I figured it out!

Would you please show how it’s done so others can learn? :slightly_smiling:

Oh, sure! :smiley:

First, here’s my content architecture:

.
β”œβ”€β”€ content
β”‚   β”œβ”€β”€ dental_work
β”‚   β”‚   β”œβ”€β”€ pediatrics.md
β”‚   β”‚   β”œβ”€β”€ young-adult.md
β”‚   β”‚   β”œβ”€β”€ etc-etc.md
β”‚   β”œβ”€β”€ careers
β”‚   β”‚   β”œβ”€β”€ cities.md
β”‚   β”‚   β”œβ”€β”€ suburban.md
β”‚   β”‚   β”œβ”€β”€ etc-etc.md
β”‚   β”œβ”€β”€ mentoring
β”‚   β”‚   β”œβ”€β”€ universities.md
β”‚   β”‚   β”œβ”€β”€ communities.md
β”‚   β”‚   β”œβ”€β”€ etc-etc.md

So, as I wrote earlier, If I am in page pediatrics.md, I want all the pages in the same section to be listed in a side menu, and the current page to be highlighted with class=active. .Site.Menu seems to fit this purpose.

First, each section gets a menu name. So, not just one menu called main as explained in the Hugo manual. Therefore, in pediatrics.md, the front matter is:

menu:
  dental_work:
    parent: "dental_work"
    weight: 1

All the content pages in dental_work must have this front matter. weight is for the ordinal display in the menu.

Pages in section careers get this front matter:

menu:
  careers:
    parent: "careers"
    weight: n

Pages in section mentoring get this front matter:

menu:
  mentoring:
    parent: "mentoring"
    weight: n

In the sidebar navigation template that I made for the page template, I have:

{{ $currentNode := . }}
{{ $thisSection := .Section }}

{{ range (index .Site.Menus $thisSection) }}
    {{ if .HasChildren }}
    <ul class="nav" id="section-list">
    {{ range .Children }}
    <li class="{{if $currentNode.IsMenuCurrent $thisSection . }} active{{end}}"><a href="{{.URL}}">{{ .Name }}</a></li>
    {{ end }}
    </ul>
    {{end}}
{{end}}

That’s it. Let me know if I’m missing anything.

-Z

2 Likes