hugo/docs/content/en/methods/menu/Limit.md
2023-12-04 15:24:01 +01:00

887 B

title description categories keywords action
Limit Returns the given menu, limited to the first N entries.
related returnType signatures
navigation.Menu
MENU.Limit N

The Limit method returns the given menu, limited to the first N entries.

Consider this menu definition:

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

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

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

To sort the entries by name, and limit to the first 2 entries:

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

Hugo renders this to:

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