hugo/docs/content/en/methods/menu/Reverse.md
2024-01-26 08:22:42 +01:00

943 B

title description categories keywords action
Reverse Returns the given menu, reversing the sort order of its entries.
related returnType signatures
navigation.Menu
MENU.Reverse

The Reverse method returns the given menu, reversing the sort order of its entries.

Consider this menu definition:

{{< code-toggle file=hugo >}} menus.main name = 'Services' pageRef = '/services' weight = 10

menus.main name = 'About' pageRef = '/about' weight = 20

menus.main name = 'Contact' pageRef = '/contact' weight = 30 {{< /code-toggle >}}

To sort the entries by name in descending order:

<ul>
  {{ range .Site.Menus.main.ByName.Reverse }}
    <li><a href="{{ .URL }}">{{ .Name }}</a></li>
  {{ end }}
</ul>

Hugo renders this to:

<ul>
  <li><a href="/services/">Services</a></li>
  <li><a href="/contact">Contact</a></li>
  <li><a href="/about/">About</a></li>
</ul>