Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering Menu items

Tags:

hugo

I'm trying to order items in a menu but It's not working out for me. I've tried to following according to their docs but it doesn't work at all.

In my header:

      {{ range .Site.Data.Menu }}
      <li>

        <a href="{{ .URL | absURL }}"
        {{ if and ( isset . "Title" ) ( ne .Title "" ) }} title="{{ .Title }}"{{ end }}>

        {{ if and ( isset . "IconClass" ) ( ne .IconClass "" )  }}
            <i class="fa {{ .IconClass }}"></i>
        {{ end }}

        {{ .Name }}
        </a>
      </li>
      {{ end }}

menu.toml

[home]
    Name = "Home"
    Title = "Home"
    URL = "/home"
    weight = 1

[apparatus]
    Name = "Apparatus"
    URL = "/apparatus"
    weight = 2

[deliveries]
    Name = "Deliveries"
    URL = "/deliveries"
    weight = 3

[command]
    Name = "Command"
    URL = "/command"
    weight = 4

[ambulance]
    Name = "Ambulance"
    URL = "/ambulance"
    weight = 5

[service]
    Name = "Service"
    URL = "/service"
    weight = 6

[about]
    Name = "about"
    URL = "/about"
    weight = 7

[contact]
    Name = "Contact"
    URL = "/contact"
    weight = 8

The menu ends up in a seemingly random order. How do I order them the way I want them?

like image 270
Fennek Avatar asked Feb 14 '16 15:02

Fennek


2 Answers

Weight is the default sort for menus in Hugo. Some variations below:

{{ range .Site.Data.Menu.Sort }}
{{ range .Site.Data.Menu.ByName }}
{{ range .Site.Data.Menu.ByName.Reverse }}
{{ range .Site.Data.Menu.ByName.Limit 10 }}
{{ range .Site.Data.Menu.ByWeight }}
like image 189
bep Avatar answered Oct 13 '22 04:10

bep


I had the same problem and solved it by using negative numbers for the weight attribute.

like image 6
Martin Avatar answered Oct 13 '22 04:10

Martin