Active class for menu and IsMenuCurrent… they drive me nuts!


OK, I read about everything I could find about menus, IsMenuCurrent (and its “Has” companion) and multilingual sites but in vain : I can’t make it work for me.

So here are my personal instances of this apparently common problem :slight_smile:
My menus are defined like this as per this topic :


	weight = 1
	languageName = "English"
	title = "MyTitle"

			url        = "/en/"
			name       = "Home"
			identifier = "home"
			weight     = -1
			url        = "/en/about_us/"
			name       = "About us"
			identifier = "about"
			weight     = 1 

Here is my menu template :

                {{ $currentPage := . }}
                {{ range .Site.Menus.main -}}
                <li {{ if  or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }}class="active"{{ end }}>
                    <a href="{{ .URL  }}">{{ .Name }}</a>
                {{- end }}

… needless to say : I don’t ever get any active link and this is the best I could get. I think I may have missed something in the front matter section of my pages but I can’t figure out what.

Any help appreciated.


Still struggling… but something changed : I noticed it actually works for one page, Home, so something with layouts maybe…


I may as well use this thread to document my progress :slight_smile: :

In the above linked topic I couldn’t figure out the meaning of those lines :

for a page to answer truthfully to IsMenuCurrent etc. it must be a member of that menu (as: It needs a menu definition in frontmatter).

This topic gave me the answer and I tried to apply it but to no avail… I guess it may have something to do with the fact that my site uses multilingual menus and so my menus definitions are different in my config file.


I’m trying too, I will report if I make any progress. I think i will abandon Hugo after this project due to the lack of good documentation.

It was two months ago and I did not use Hugo since so I don’t remember all the details but you must define your multilingual menus items using frontend matter in your pages like this :, french version front matter

date = "2017-05-18T11:47:51+02:00"
draft = false
title = "À propos d'Angelze"
name = "À propos"
weight = 1
+++, english version front matter

date = "2017-05-18T11:47:51+02:00"
draft = false
title = "About us"
name = "About us"
weight = 1

Using weights to sort your items.
It worked for me that way and I did get active links.
Didn’t check with the latest version though.