Menu Highlighting

I’m new to Hugo.
I’m trying to keep the current page/section highlighted in the top menu.

I’ve tried the solutions suggested

  1. here

  2. here

  3. and here from stackoverflow :

Then in /layouts/partials/menu.html I have tried the solutions suggested in the above links:

<!-- @ian copied this from:-->
<!-- -->
{{ $currentPage := . }}
{{ range .Site.Menus.main }}

    {{ $menu_item_url := .URL | relLangURL }}
    {{ $page_url:= $currentPage.RelPermalink | relLangURL }}

    {{ if eq $menu_item_url $page_url }}
        {{/* the menu item links to the current page (with relLangURL) */}}
    {{ end }}

{{ end }}

<!-- @ian copied this from:-->
<!-- -->

{{ $currentPage := . }}
{{ range .Site.Menus.main }}
    <a href="{{ .URL }}" 
        {{ if or ($currentPage.IsMenuCurrent "main" .) (eq $currentPage.Section .Identifier) }}
        {{ end }}
        {{ .Name }}</a>
{{ end }} 


I change my config to be:

    name = "Home"
    pageref = "/"
    weight = 1
    name = "About"
    pageref = "/about/about/"
    weight = 2

Then inside /layouts/partials/menu.html

{{ $currentPage := . }}

{{ range .Site.Menus.main }}

<li class='menu-item{{if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }} current-menu-item{{end}}'>

<a title="{{ .Name }}" href="{{ .pageref }}">{{ .Name }}</a>


{{ end }}

My menus stopped working completely?

In site configuration you defined a menu named “primary”.

But in your partial you are ranging through a menu named “main”.

Which is it?

Thanks Hugo.
Changed them both do main. Didn’t seem to make a difference.

Can you share your repository?


Let us see your code

Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick git clone on your repo, then run hugo server in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.

If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.

Its a private repository. I can add you?


Thanks Jo.

1) In your site configuration, you have placed sectionPageMenu = "Main" within the [params] table, so it is ignored. If you want to enable this feature, move that line into the root table (i.e., above all other tables).

2) When you specify pageref in a menu entry in your site configuration, it needs to match a page. When specifying a pageref for content/About/, any of these will work:

pageref = "/About/"
pageref = "/About/about"
pageref = "About/"
pageref = "About/about"

What you currently have does not work because of the trailing slash:

pageref = "/about/about/"

3) Using uppercase characters in file and directory names is generally a bad idea. Use lowercase characters, and be consistent.

4) The the hugo-tanka theme ranges through the “primary” menu in its header:

If you want to display that menu, modify the menu entries in your site configuration, change [[menu.main]] to [[menu.primary]] for all menu entries.

Or, if you want to use the “Section Menu for Lazy Bloggers”, make the change noted in #1 above, and change the menu name: sectionPagesMenu = "primary"

5) You created two templates in layouts/partials/ but these are never called, which is good thing because both are broken.

  • In layouts/partials/menu.html you tried to “comment out” a lot of template code by wrapping it within HTML comment tags (<!-- -->). That doesn’t do what you think it does. Use Go template comments instead.
  • In layouts/partials/sidebar.html you have a complete HTML page (DOCTYPE, <html>, <head>, and <body> elements. No. A partial is a… partial.

I know your question was about highlighting active menu entries, but with the current state of your project, I really have no idea what you want.

  • Do you want to use the header menu provided by theme, or write your own?
  • Do you want menu entries determined automatically with the “Section Menu for Lazy Bloggers” feature, or do you want to manually define them in site configuration?
  • Do you want the menu named “main” or “primary”?

Thank Joe, for taking the time to look over my, admittedly naive code.
I answer to your questions:

Use the themes for now.

Sections menu should be enough for now.

No preference.

I’ve made some additional changes:

  • moved the "sectionPagesMenu = “primary” above the [params] in config.
  • I went for the ‘lazy bloggers menu’ and (thanks to you) got it working.

@jmooring How do you call a template?

Blockquote 5) You created two templates in layouts/partials/ but these are never called,

I got it working.

This code underlines the menu item:
(From layouts/Partials/header.html)

        {{ if or ($.IsMenuCurrent "primary" .) (eq $ .Name ) }}
        {{ end }}