HUGO
News Docs Themes Community GitHub

Limit

Returns the given menu, limited to the first N entries.

Syntax

MENU.Limit N

Returns

navigation.Menu

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

Consider this menu definition:

menus:
  main:
  - name: Services
    pageRef: /services
    weight: 10
  - name: About
    pageRef: /about
    weight: 20
  - name: Contact
    pageRef: /contact
    weight: 30
[menus]
  [[menus.main]]
    name = 'Services'
    pageRef = '/services'
    weight = 10
  [[menus.main]]
    name = 'About'
    pageRef = '/about'
    weight = 20
  [[menus.main]]
    name = 'Contact'
    pageRef = '/contact'
    weight = 30
{
   "menus": {
      "main": [
         {
            "name": "Services",
            "pageRef": "/services",
            "weight": 10
         },
         {
            "name": "About",
            "pageRef": "/about",
            "weight": 20
         },
         {
            "name": "Contact",
            "pageRef": "/contact",
            "weight": 30
         }
      ]
   }
}

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>