| 1 |
--- |
| 2 |
title: Menus |
| 3 |
description: Create menus by defining entries, localizing each entry, and rendering the resulting data structure. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
aliases: [/extras/menus/] |
| 7 |
--- |
| 8 |
|
| 9 |
## Overview |
| 10 |
|
| 11 |
To create a menu for your site: |
| 12 |
|
| 13 |
1. Define the menu entries |
| 14 |
1. [Localize](multilingual/#menus) each entry |
| 15 |
1. Render the menu with a [template][] |
| 16 |
|
| 17 |
Create multiple menus, either flat or nested. For example, create a main menu for the header, and a separate menu for the footer. |
| 18 |
|
| 19 |
There are three ways to define menu entries: |
| 20 |
|
| 21 |
1. Automatically |
| 22 |
1. In front matter |
| 23 |
1. In your project configuration |
| 24 |
|
| 25 |
> [!NOTE] |
| 26 |
> Although you can use these methods in combination when defining a menu, the menu will be easier to conceptualize and maintain if you use one method throughout the site. |
| 27 |
|
| 28 |
## Define automatically |
| 29 |
|
| 30 |
To automatically define a menu entry for each top-level [section](g) of your site, enable the section pages menu in your project configuration. |
| 31 |
|
| 32 |
{{< code-toggle file=hugo >}} |
| 33 |
sectionPagesMenu = 'main' |
| 34 |
{{< /code-toggle >}} |
| 35 |
|
| 36 |
This creates a menu structure that you can access with `site.Menus.main` in your templates. See [menu templates][] for details. |
| 37 |
|
| 38 |
## Define in front matter |
| 39 |
|
| 40 |
To add a page to the "main" menu: |
| 41 |
|
| 42 |
{{< code-toggle file=content/about.md fm=true >}} |
| 43 |
title = 'About' |
| 44 |
menus = 'main' |
| 45 |
{{< /code-toggle >}} |
| 46 |
|
| 47 |
Access the entry with `site.Menus.main` in your templates. See [menu templates][] for details. |
| 48 |
|
| 49 |
To add a page to the "main" and "footer" menus: |
| 50 |
|
| 51 |
{{< code-toggle file=content/contact.md fm=true >}} |
| 52 |
title = 'Contact' |
| 53 |
menus = ['main','footer'] |
| 54 |
{{< /code-toggle >}} |
| 55 |
|
| 56 |
Access the entry with `site.Menus.main` and `site.Menus.footer` in your templates. See [menu templates][] for details. |
| 57 |
|
| 58 |
> [!NOTE] |
| 59 |
> The configuration key in the examples above is `menus`. The `menu` (singular) configuration key is an alias for `menus`. |
| 60 |
|
| 61 |
### Properties |
| 62 |
|
| 63 |
Use these properties when defining menu entries in front matter: |
| 64 |
|
| 65 |
{{% include "/_common/menu-entry-properties.md" %}} |
| 66 |
|
| 67 |
### Example |
| 68 |
|
| 69 |
This front matter menu entry demonstrates some of the available properties: |
| 70 |
|
| 71 |
<!-- markdownlint-disable MD033 --> |
| 72 |
{{< code-toggle file=content/products/software.md fm=true >}} |
| 73 |
title = 'Software' |
| 74 |
[menus.main] |
| 75 |
parent = 'Products' |
| 76 |
weight = 20 |
| 77 |
pre = '<i class="fa-solid fa-code"></i>' |
| 78 |
[menus.main.params] |
| 79 |
class = 'center' |
| 80 |
{{< /code-toggle >}} |
| 81 |
<!-- markdownlint-enable MD033 --> |
| 82 |
|
| 83 |
Access the entry with `site.Menus.main` in your templates. See [menu templates][] for details. |
| 84 |
|
| 85 |
## Define in project configuration |
| 86 |
|
| 87 |
See [configure menus][]. |
| 88 |
|
| 89 |
## Localize |
| 90 |
|
| 91 |
Hugo provides two methods to localize your menu entries. See [multilingual][]. |
| 92 |
|
| 93 |
## Render |
| 94 |
|
| 95 |
See [menu templates][]. |
| 96 |
|
| 97 |
[configure menus]: /configuration/menus/ |
| 98 |
[menu templates]: /templates/menu/ |
| 99 |
[multilingual]: /content-management/multilingual/#menus |
| 100 |
[template]: /templates/menu/ |