Skip to main content

Add navigation to your theme

Merchants can create menus for their shop navigation, and these menus can be nested to create drop-down menus.

In this tutorial, you'll learn how to add navigation to your theme.



Anchor to Implementing navigationImplementing navigation

To add navigation to your theme, you should reference a linklist object. Each linklist object represents a menu that's defined in the Online Store > Navigation section of the Shopify admin.

You can use the global linklists object to access all of the linklist objects in your store by their handle. The default menu in the Shopify admin is the Main menu, which can be accessed with its handle main-menu.

For example:

{% for link in linklists.main-menu.links %}
<!-- menu content -->
{% endfor %}

You can let merchants select their own menu using the link_list setting. You can reference the menu using the setting name, which is the equivalent of a linklist handle:

{% for link in section.settings.menu.links %}
<!-- menu content -->
{% endfor %}

For each menu link, you should output information such as the title and URL. You might also want to output the link's child links. You can nest links up to three levels deep, and you can access them through the links attribute of the link object.

For example, if you've created a link_list type setting called menu, so that merchants can choose the menu they want to use in the header section, then the following code shows how you might output the menu.

Note

The following example is only meant to illustrate how to iterate through a linklist and output multiple levels of links. It's not a complete navigation feature.

/sections/header.liquid

<ul className="menu">
{% for link in section.settings.menu.links %}
<li className="menu-link">
<a href="{{ link.url }}">{{ link.title }}</a>

{% if link.links.size > 0 %}
<ul className="menu dropdown-child">
{% for child_link in link.links %}
<li className="menu-link">
<a href="{{ child_link.url }}">{{ child_link.title }}</a>

{% if child_link.links.size > 0 %}
<ul className="menu dropdown-grandchild">
{% for grandchild_link in child_link.links %}
<li className="menu-link">
<a href="{{ grandchild_link.url }}">{{ grandchild_link.title }}</a>
</li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>

Depending on the kind of navigation you're building, you should include your navigation code in your header or footer sections.

Tip

For another example of outputting menus, you can refer to Dawn's implementation.


Was this page helpful?