In my navigation.html partial I have a conditional
<nav>
{{ $currentPage := . }}
{{ range .Site.Menus.main }}
<a class="nav-links{{if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }} active{{end}}" href="{{.URL}}">{{ .Name }}</a>
{{ end }}
</nav>
However, the first item in the menu “Home” is highlighted and it gets the class of active, but when I click on any other page, those links don’t get the active class. The home does lose the class when it’s not active.
I am still confused, the documentation isn’t that clear on Hugo. I would like to understand this better.
Actually it’s a choice, I am still discovering Hugo. If I don’t mention anything in the config file and just add some front matter in pages like menu: “main”, weight: “2” for example, it still shows up in the top main navigation
Thanks. Can you provide some full example? Because now I’m using in config.toml it renders ok with nested items.
When I put menu item in frontmatter it displays well but I have issues with nested items and checking what item is active now.
It’s possible to do this without JavaScript which is (in my eyes) a better solution, for the following reasons:
visitors that have JavaScript turned off will still see the correct result
because JavaScript can sometimes be executed later (for network or other reasons) the user will first see the menu without the highlighted current link and then see that link light up once the JavaScript runs.