[SOLVED] Menu ordering not working

#1

Hi,

I’m trying to create a super simple site - 4 pages, which I created as .md files in the content folder.

The pages appear in the menu in alphabetical order - but I need them to be ordered differently.

I read the docs about menus and tried everything in there (as well having the weight set in pages front matter, and also having extra menu entries defined in config.toml) - but none of these have any effect.
No changed order, no extra menu entries. No errors on build - which I would expect if the tool sees things that it doesn’t know and doesn’t want to execute.

I’d hope this is possible without further hacking of templates etc, but anything I google about menus and orders gives me complex instructions that involve hacking custom templates etc. which I want to prevent for a simple 4 page site.

Most importantly, I’d expect a page with this front matter to appear in the last place of the menu:

   title: "Impressum"
    date: 2017-10-26T23:21:59+01:00
    draft: false

    menu:
      main:
        weight: -200
        
    weight: -200

Any idea what I could be doing wrong?

I’m using the “zen” theme - please let me know if this question should better be asked to the theme developer than here.

#2

We need a link to your site’s code repo to troubleshoot. Please see Requesting Help.

#4

Repo: https://github.com/henning/hugo-website

hugo env:

$ hugo env
Hugo Static Site Generator v0.40.3 darwin/amd64 BuildDate: 
GOOS="darwin"
GOARCH="amd64"
GOVERSION="go1.10.2"
#5

Are the pages appearing in the menu without setting it in frontmatter? If so just try setting weight?

#6

Thanks Rick,

Yes the pages appear in the menu.
Not sure what you mean exactly with “setting it” - they appear in the menu immediately when the .md files are created, they just need the “title” variable set to have a proper name in the menu.

Then the menu is ordered by title alphabetically.

I have set the weight, in the way posted in the code block in my first message - but sorry, I just realised the version i pushed to git didn’t have an example of my trials to set weight. Updated that now.
Now all pages have weights set, but order is the same.

I tried things like the frontmatter shown in the first posts code block, to no effect, I tried many other things described in the hugo docs about the menu stuff. That’s when I came here…

#7

You should consider asking the theme author. I don’t think that theme incorporates the Hugo menu system.

The good news is, you can just use weight by itself. In the front matter, just use weight by itself, not as part of a menu parameter. I believe it does what you want.

The way the menu in the zen theme is populated is just by listing all pages. I’m explaining it this way because the Hugo menu system is really useful, and therefore flexible to do things like create contextual menus, in sub-sections, all that jazz.

But to just create an arbitrary list of links, that can be done in lots of ways, and in this instance qualifies for a “menu” as a web navigational element. :slight_smile:

#8

Looks like Zen is using this partial:

… for the menu. It’s checking for any content without a section and listing those, then doing range $.Site.Home.Sections.

I have not tried this recently but, you should be able to copy that to your project’s layouts/partials, then edit to override.

The range command is supposed to sort by weight as the default but, you can force it like range where .Site.Pages.ByWeight.

This might be voodoo magic based on past bugs, but, I vaguely recall something about menus needing to be at the bottom of the frontmatter, so I have superstitiously always set it that way. So maybe try:

---
title: "Impressum"
date: 2017-10-26T23:21:59+01:00
draft: false
weight: -200
menu:
    main:
        weight: -200
---

Also I could be wrong but I don’t see anything in the docs about negative values for weight. Not sure if that is valid.

#9

I did consider that(as the quote suggests :wink: ) - but I had to start somewhere…

But I tried that (see sources) and it had no effect…

Sounds to me like hugo is extremely powerful to make complex and demanding stuff, at the same time it seems to require learning everything even for doing simple things. I hoped it would be possible to do simple things easier (while complex things are possible after going deeper into the details)…

#10

Thanks a lot!
I think I’m slowly getting it.
Experimenting a bit more, changing the place where I put the “weight” value the order actually started to change the first time… but I also had to understand by experimenting, that “ordering by weight” means the lower weight comes first - which I see not documented anywhere (at least it’s not mentioned either here https://gohugo.io/content-management/front-matter/ or there https://gohugo.io/templates/lists/ where things are said about weight).
I wrongly misinterepreted weight as similar to “importance” and more important stuff comes first… (created two doc pull requests to help others understand that quicker).
Then I even tried removing the “menu:” section completely, just keeping the weight, and that works too.

By the way - negative weights are mentioned in the menu docs (mentioned above, this forum system prevents me of adding more than two links :frowning:

Thanks a lot, now things are working exactly as I need them! :wink: