Skip to content
This repository has been archived by the owner on Jun 10, 2023. It is now read-only.

use menu entry property weight to sort menu items #276

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,18 @@ paginate = 5
# path = "/img/your-example-logo.svg"
# alt = "Your example logo alt text"

# `weight` is used to sord menu items. https://gohugo.io/variables/menus/
[languages.en.menu]
[[languages.en.menu.main]]
identifier = "about"
name = "About"
url = "/about"
weight = 1
[[languages.en.menu.main]]
identifier = "showcase"
name = "Showcase"
url = "/showcase"
weight = 0
```

to `config.toml` file in your Hugo root directory and change params fields. In case you need, here's [a YAML version](https://gist.github.com/panr/8f9b363e358aaa33f6d353c77feee959).
Expand Down
10 changes: 5 additions & 5 deletions layouts/partials/menu.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<nav class="menu">
<ul class="menu__inner menu__inner--desktop">
{{ if or $.Site.Params.showMenuItems (eq $.Site.Params.showMenuItems 0) }}
{{ range first $.Site.Params.showMenuItems $.Site.Menus.main }}
{{ range first $.Site.Params.showMenuItems (sort $.Site.Menus.main "Weight") }}
{{ if not .HasChildren }}
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
{{ end }}
Expand All @@ -15,16 +15,16 @@
>
</li>
<ul class="menu__sub-inner-more hidden">
{{ range last (sub (len $.Site.Menus.main) $.Site.Params.showMenuItems) $.Site.Menus.main }}
{{ range last (sub (len $.Site.Menus.main) $.Site.Params.showMenuItems) (sort $.Site.Menus.main "Weight") }}
{{ if not .HasChildren }}
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
{{ end }}
{{ end }}
</ul>
</ul>
{{ end }}
{{ else }}
{{ range $.Site.Menus.main }}
{{ else }}
{{ range sort $.Site.Menus.main "Weight" }}
{{ if not .HasChildren }}
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
{{ end }}
Expand All @@ -33,7 +33,7 @@
</ul>

<ul class="menu__inner menu__inner--mobile">
{{ range $.Site.Menus.main }}
{{ range sort $.Site.Menus.main "Weight" }}
{{ if not .HasChildren }}
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
{{ end }}
Expand Down