Modify menu items in template

I am designing a page based on the hugo docs “theme” with a two-level menu.
I am fine with defining menu items through both frontmatter and config.toml.

I do want to define menu items depending on the content in more sophisticated fashion, like:
I have an archetype “release” and I want to create menu entries for the 3 latest such releases.
AFAICS this is neither possible through the frontmatter nor though config,toml.

I have tried hacking it into the menu template of the hugo docs, and I can get it to look okay with something like this:

{{ if eq .Identifier "releases" }}
  {{ range first 3 (where $.Site.Pages "Type" "releases").ByTitle.Reverse }}
    <li><a href="{{.RelPermalink}}">v{{ .Title }}</a> </li>
  {{ end }}
{{ end }}

However, the menu folds again upon clicking it. To avoid that I would need to treat these additional child menu nodes just as all the others are treated:

{{ range .Children }}
  <li{{if $currentNode.IsMenuCurrent "main" . }} class="active"{{end}}><a href="{{.URL}}">{{ .Name }}</a> </li>
{{ end }}

Unfortunately, I cannot see how to transform these into proper tree nodes in a go template. I should probably mention that I have no clue of Go as a language. Anyone to the rescue? TIA!

Sorry no one has responded yet. It’s not clear to me what you’re asking for. Are you saying that you don’t know how to make the first code section use IsMenuCurrent?

Yes, that kind of was my problem. That first code operates on Pages instead of menu trees, so I thought I need to construct menu nodes in the template.

However, a good nights sleep helped me solve the problem: I now define a menu item for very release through the front matter and discard those in the template that are too old. Much easier this way.

1 Like