mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-29 14:42:09 -05:00
51 lines
890 B
Markdown
51 lines
890 B
Markdown
|
---
|
||
|
title: Limit
|
||
|
description: Returns the given menu, limited to the first N entries.
|
||
|
categories: []
|
||
|
keywords: []
|
||
|
action:
|
||
|
related: []
|
||
|
returnType: navigation.Menu
|
||
|
signatures: [MENU.Limit N]
|
||
|
---
|
||
|
|
||
|
The `Limit` method returns the given menu, limited to the first N 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, and limit to the first 2 entries:
|
||
|
|
||
|
```go-html-template
|
||
|
<ul>
|
||
|
{{ range .Site.Menus.main.ByName.Limit 2 }}
|
||
|
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
|
||
|
{{ end }}
|
||
|
</ul>
|
||
|
```
|
||
|
|
||
|
Hugo renders this to:
|
||
|
|
||
|
```html
|
||
|
<ul>
|
||
|
<li><a href="/about/">About</a></li>
|
||
|
<li><a href="/contact">Contact</a></li>
|
||
|
</ul>
|
||
|
```
|