The “Blog” menu item is active when viewing the list page URL:
/post/
But not when viewing a single page URL:
/post/some-blog-post/
I solved it checking if the current menu iteration name was “Blog” and if the current section was “post”. I did the same thing for my “Tags” pages. Here’s a stripped down version of the solution:
{{ $current := . }}
{{ range .Site.Menus.nav }}
{{ $active := or ($current.IsMenuCurrent "nav" .) ($current.HasMenuCurrent "nav" .) }}
{{ $active = or $active (eq .Name $current.Title) }}
{{ $active = or $active (and (eq .Name "Blog") (eq $current.Section "post")) }}
{{ $active = or $active (and (eq .Name "Tags") (eq $current.Section "tags")) }}
<a href="{{ .URL }}" class="{{ if $active }}active{{ end }}">{{ .Name }}</a>
{{ end }}
Here’s the full nav partial and the full menu config. You can see it in action here by clicking any of the blog posts and noticing the “Blog” menu is still set to active.
If others know of a better way to solve this, please do share.
Note that were you menus config added through individual page’s Front Matter, you could access any Menu item’s page with .Page, and therefor perform more faithful and generic tests than with .Name.
Per example instead of testing the menu item’s .Name against the name of current page’s section, you could compare the sections.
{{ $active := or ($current.IsMenuCurrent "nav" .) ($current.HasMenuCurrent "nav" .) }}
{{ $active = or $active (eq .Page $current) }}
{{ $active = or $active (eq .Page.Section $current.Section)) }}
Unfortunately that doesn’t work as expected. This is a screenshot of the menu while on the “Blog” page (only the Blog menu item should be highlighted).
For debugging, I’ve printed the path.Dir for each menu item.
@ zwbetz: You are right. I had no Home nor any other root link in my menu, so it was working for me. I guess a check if the menu item belongs to Kindsection or page would also work. But that’s again adding extra lines. My example above made using .IsMenuCurrent, .HasMenuCurrent obsolete, so it was just a one liner. That’s why I suggested it, but as you pointed out, it is not working for every use case.
This looks correct. .URL | relLangURL and .RelPermalink are identical. Do they both change when you click on another link? (You posted a link to localhost.)