Adding Navigation Menu Items in Hugo Using YAML
A user-friendly navigation menu is key to guiding visitors through your website, and Hugo, the popular static site generator, makes it easy to manage menus using YAML configuration. This guide will walk you through the steps to add and customize menu items for your Hugo site.
Step 1: Update the Configuration File
Start by locating your site’s configuration file (config.yaml
for YAML setups). Define your navigation menu items under the menu
section like this:
menu:
main:
- identifier: home
name: Home
url: /
weight: 1
- identifier: about
name: About
url: /about/
weight: 2
- identifier: contact
name: Contact
url: /contact/
weight: 3
- Identifier: Unique ID for internal reference.
- Name: The visible label for the menu item.
- URL: The destination link for the menu item.
- Weight: Controls the display order—lower numbers appear first.
Step 2: Add Menu Metadata to Content Files (Optional)
To automatically include pages in the menu, add metadata to the front matter of your content files. For example, add the following to about.md
:
---
title: "About"
menu:
main:
weight: 2
---
This ensures the page is listed in the main menu without requiring manual configuration in config.yaml
.
Step 3: Verify Template Supports Menus
Make sure your theme includes support for navigation menus. Most themes do, but it’s good to confirm. Check templates like header.html
or nav.html
for menu-related code. A typical example might look like this:
<nav>
<ul>
{{ range .Site.Menus.main }}
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
{{ end }}
</ul>
</nav>
If your theme doesn’t support menus, you can customize it by adding this code snippet to render the menu dynamically.
Step 4: Build and Test
Once your menu configuration is set:
- Preview your changes locally by running:
hugo server
- Visit
http://localhost:1313
to verify that:- Menu items display correctly.
- Links point to the intended pages.
Conclusion
Adding navigation menu items in Hugo using YAML is an effective way to enhance your site’s usability and structure. By defining menus in config.yaml
and leveraging front matter for dynamic inclusion, you ensure your site is easy to navigate and adaptable to future changes.
And for a touch of humor to end this post:
When Chuck Norris writes a YAML file, he doesn’t need to worry about indentation. The lines fall in line out of respect.