Navigation > active item only partially working

I don’t get my menu design to work. The menu is ok, but marking the currently active item is a bit hard.

I have the following structure in my content:


Simply said I would like to mark a menu item active if it’s the current page (inside of the page folder) or if the menu item is the archive (either list pages for posts, taxonomies or single items) or we see a post (one of the items inside of content/post).

I would have expected the following piece to work:

        {{ $currentPage := . }}
        {{ range .Site.Menus.main }}
          <li class="nav-item{{if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) (and (eq $currentPage.Type "post") (eq .Identifier "archiv" )) }} active{{end}}">
            <a href="{{.URL}}"{{if eq .Page nil }} target="_blank" rel="noopener noreferrer"{{ end }} class="nav-link">
              <span class="icon larger light" aria-hidden="true">
                {{ partialCached (print .Pre) .}} 
              {{ .Name }}
        {{ end }}

It doesn’t though. The archive link is ALWAYS marked active. (The menu entry is in page frontmatter and it’s identifier is “archiv” - German ;] ).

What would be the correct way to use a whole section as an indicator? I tried .Section, but that makes ALL links in the nav active.

The front matter in the page-folder explicitly marks them as type: page.

The repo is here:

This may help: Another way to make a menu item active

1 Like

I’m not quite entirely sure yet, but it may be your use of partialCached to reference the header (therefore navbar, therefore menu)?

As a test: printing out {{.}} from within the nav partial vs from the baseof and checking the results in your kontakt page sometimes gives me different results. And depending on which page gets printed from inside the nav partial, different menu items get set to active.


Oh. My. God. That would explain about 10 of my current problems. And I don’t know how the cached got there. Thanks for that :slight_smile: I will go hide and bow my head in shame for about an hour or so.

1 Like