IsMenuCurrent doesn't work for me

Hi guys, I tried to highlight current menu and it doesn’t work for me. I checked docs and forum and I did not find answer.

  1. I made fresh clean hugo page (version 0.36.1)
  2. I created a.md, b.md and c.md pages
  3. This is my code:

config.toml

baseURL = "http://example.org/"
languageCode = "en-us"
title = "My New Hugo Site"

[[menu.main]]
  name = "Home"
  identifier = "home"
  url = "/"
  weight = 10
[[menu.main]]
  name = "A"
  identifier = "a"
  url = "/a/"
  weight = 20
[[menu.main]]
  name = "B"
  identifier = "b"
  url = "/b/"
  weight = 30
[[menu.main]]
  name = "C"
  identifier = "c"
  url = "/c/"
  weight = 40

single html

<!doctype html>
<html lang="en">
<head>
  <title>test</title>
</head>
<body>
  <h1>{{.Title}}</h1>
  <div>{{.Content}}</div>

  {{ $currentPage := . }}
  <ul>
    {{ range .Site.Menus.main }}
      <li>
        {{ .Name }}
        | IsMenuCurrent = {{ $currentPage.IsMenuCurrent "main" . }} |
        | $currentPage = {{ $currentPage }}
        | menuItem = {{ . }}
      </li>
    {{ end }}
  </ul>
</body>
</html>

and result for menu B is:

B
bbb

Home | IsMenuCurrent = false | | $currentPage = Page("B") | menuItem = {/ <nil> Home main home 10 []}
A | IsMenuCurrent = false | | $currentPage = Page("B") | menuItem = {/a/ <nil> A main a 20 []}
B | IsMenuCurrent = false | | $currentPage = Page("B") | menuItem = {/b/ <nil> B main b 30 []}
C | IsMenuCurrent = false | | $currentPage = Page("B") | menuItem = {/c/ <nil> C main c 40 []}

B should be current menu but is not.

Do you have any idea where is a problem?
Thanks.

1 Like

Were you able to find a solution? I’m hitting the same thing.

Here’s how I do it in my main menu:

<div class="navbar-item has-dropdown is-hoverable {{ if $currentPage.HasMenuCurrent "main" . }}has-text-weight-bold{{ end }}" aria-haspopup="true">

The only minor problem with that is if you are using drop-down menus as a page with a sub-menu will also have the main menu item as well so everything is bold. I’ll get round to improving it one day but as I’ve only just started to migrate from WordPress, I have more important things to fix first.