hugo/content/en/content-management/menus.md
Bjørn Erik Pedersen a6e635ca7d Squashed 'docs/' changes from 9b06f951e..fcc3ed651
fcc3ed651 Remove some expired new-in
a9c5981f5 Fix cascade example
82bb250fa Add some lines about permalinks tokens in front matter
328fe564e Remove some outdated new-in
fb140b153 Hide showcase menu entry
42d9d1c79 Update image formats from which EXIF data can be extracted
09ad56b6e netlify: Hugo 0.130.0
1d503f846 Merge branch 'tempv0.130.0'
e2458074d math: Add trigonometric functions and some angle helper functions
392afc8f9 Disable the showcase section for now
0300750f2 Improve example of image render hook
60a9306af Improve description of the .Site.RegularPages method
8d759175d Fix typos
55daa4554 Update XxHash.md
397c81cb7 Add namespace for hash functions
70fe8d2f0 netlify: Bump Hugo 0.129.0
5a9771aff Merge branch 'tempv0.129.0'
f9146575b Fix typo
e6e1fea49 Fix typo in Hugo docs | functions | partial
732d10ec4 source: Expose GitInfo Body
34c97e639 netlify: Hugo 0.128.2
3270587e9 Fix typo
727c5396e netlify: Hugo 0.128.1
80b6ae99c Update GitHub Pages workflow file example
027134102 Update GitHub Pages workflow file example
2600a8a2e Miscellaneous edits
3fdd5819b Update Build.md
7764005c3 Improve example of render hook directory structure
5e3941d82 Fix typos
748bf065f Restructure templates section
fafbf6566 Update Defer.md
012162e0d Document changes to template functions in v0.128.0
0990ce35b quick-reference: Update emojis
6677a30ef Update Goldmark configuration documentation
4449d530d Document new pagination config
0af8be439 Update Defer.md
56348196d netlify: Hugo 0.128.0
d67b6d82e Update content/en/functions/templates/Defer.md
23d996b3d Update content/en/functions/templates/Defer.md
7f7fb2f27 Document templates.Defer
5ada1e9d5 Fix docs merge (remove shortcode)
d27ee6156 Merge branch 'tempv0.128.0'
5d7317c84 Fix typo
7c18ee546 Update theme
83bfea63b Update theme
b274b3238 Merge commit '8b9803425e63e1b1801f8d5d676e96368d706722'
ff34a035a deploy: Add stripIndexHtml target option
d9e964bdb markup/goldmark: Add the Hugo Goldmark Extras "delete" extension
ac5bd16d2 deps: Upgrade github.com/alecthomas/chroma v2.13.0 => v2.14.0
25377171b config: Remove extraneous BuildConfig setting
0d2044f6d docs: Regen docshelper
a2548dac9 markup/goldmark: Support extras extension
9d0c86ee8 commands: Add gen chromastyles --lineNumbersTableStyle flag

git-subtree-dir: docs
git-subtree-split: fcc3ed651a1b6431303c2f88f20fa38531c52b3d
2024-08-09 15:17:43 +02:00

6.1 KiB

title description categories keywords menu weight toc aliases
Menus Create menus by defining entries, localizing each entry, and rendering the resulting data structure.
content management
menus
docs
parent weight
content-management 190
190 true
/extras/menus/

Overview

To create a menu for your site:

  1. Define the menu entries
  2. Localize each entry
  3. Render the menu with a template

Create multiple menus, either flat or nested. For example, create a main menu for the header, and a separate menu for the footer.

There are three ways to define menu entries:

  1. Automatically
  2. In front matter
  3. In site configuration

{{% note %}} 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. {{% /note %}}

Define automatically

To automatically define a menu entry for each top-level section of your site, enable the section pages menu in your site configuration.

{{< code-toggle file=hugo >}} sectionPagesMenu = "main" {{< /code-toggle >}}

This creates a menu structure that you can access with site.Menus.main in your templates. See menu templates for details.

Define in front matter

To add a page to the "main" menu:

{{< code-toggle file=content/about.md fm=true >}} title = 'About' menus = 'main' {{< /code-toggle >}}

Access the entry with site.Menus.main in your templates. See menu templates for details.

To add a page to the "main" and "footer" menus:

{{< code-toggle file=content/contact.md fm=true >}} title = 'Contact' menus = ['main','footer'] {{< /code-toggle >}}

Access the entry with site.Menus.main and site.Menus.footer in your templates. See menu templates for details.

{{% note %}} The configuration key in the examples above is menus. The menu (singular) configuration key is an alias for menus. {{% /note %}}

Properties

Use these properties when defining menu entries in front matter:

identifier
(string) Required when two or more menu entries have the same name, or when localizing the name using translation tables. Must start with a letter, followed by letters, digits, or underscores.
name
(string) The text to display when rendering the menu entry.
params
(map) User-defined properties for the menu entry.
parent
(string) The identifier of the parent menu entry. If identifier is not defined, use name. Required for child entries in a nested menu.
post
(string) The HTML to append when rendering the menu entry.
pre
(string) The HTML to prepend when rendering the menu entry.
title
(string) The HTML title attribute of the rendered menu entry.
weight
(int) A non-zero integer indicating the entry's position relative the root of the menu, or to its parent for a child entry. Lighter entries float to the top, while heavier entries sink to the bottom.

Example

This front matter menu entry demonstrates some of the available properties:

{{< code-toggle file=content/products/software.md fm=true >}} title = 'Software' menus.main parent = 'Products' weight = 20 pre = '' [menus.main.params] class = 'center' {{< /code-toggle >}}

Access the entry with site.Menus.main in your templates. See menu templates for details.

Define in site configuration

To define entries for the "main" menu:

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

menus.main name = 'Products' pageRef = '/products' weight = 20

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

This creates a menu structure that you can access with site.Menus.main in your templates. See menu templates for details.

To define entries for the "footer" menu:

{{< code-toggle file=hugo >}} menus.footer name = 'Terms' pageRef = '/terms' weight = 10

menus.footer name = 'Privacy' pageRef = '/privacy' weight = 20 {{< /code-toggle >}}

This creates a menu structure that you can access with site.Menus.footer in your templates. See menu templates for details.

{{% note %}} The configuration key in the examples above is menus. The menu (singular) configuration key is an alias for menus. {{% /note %}}

Properties

{{% note %}} The properties available to entries defined in front matter are also available to entries defined in site configuration.

{{% /note %}}

Each menu entry defined in site configuration requires two or more properties:

  • Specify name and pageRef for internal links
  • Specify name and url for external links
pageRef
(string) The logical path of the target page, relative to the content directory. Omit language code and file extension. Required for internal links.
Kind pageRef
home /
page /books/book-1
section /books
taxonomy /tags
term /tags/foo
url
(string) Required for external links.

Example

This nested menu demonstrates some of the available properties:

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

menus.main name = 'Hardware' pageRef = '/products/hardware' parent = 'Products' weight = 1

menus.main name = 'Software' pageRef = '/products/software' parent = 'Products' weight = 2

menus.main name = 'Services' pageRef = '/services' weight = 20

menus.main name = 'Hugo' pre = '' url = 'https://gohugo.io/' weight = 30 [menus.main.params] rel = 'external' {{< /code-toggle >}}

This creates a menu structure that you can access with site.Menus.main in your templates. See menu templates for details.

Localize

Hugo provides two methods to localize your menu entries. See multilingual.

Render

See menu templates.