Building Nested Navigations with Shopify Link List

shopify link list

嵌套的导航是一个受欢迎的solution for effectively organizing collections, products, and pages. A Shopify link list is a simple collection of links, and these items can be created to point to a collection, page, or product within Shopify, or to a URL outside of the store’s domain.

Link lists are used for a variety of different use cases. In this tutorial, we will cover how to use link lists in a Shopify theme to create a nested navigation using an unordered list. By using link lists and Liquid, we'll have full control over the menu from within the admin, giving flexibility to the merchant running the store.

Grow your business with the Shopify Partner Program

Whether you offer marketing, customization, or web design and development services, the Shopify Partner Program will set you up for success. Join for free and access revenue share opportunities, tools to grow your business, and a passionate commerce community.

Sign up

Create a development store

Before we start let’s set up a development store that we can work with. That way we can test, break, and fix things as we build, and once we’re happy with our code we can move it into production.

Creating a nested navigation

2017年,Shopify添加的能力create a nested navigation menu, up to three levels deep from a single page, by using a new menu editing interface. Previously, menus were created using multiple menus, and the handle for each menu to tie it to its parent menu link.

At the time of writing this article, all newly created stores have the new nested menus user interface, where you can easily drag, drop, and nest menu items, meaning you no longer have to link handles to menu names.

While it's common to include the navigation in a layout file, the default one beingtheme.liquid, you can test out the nested navigation concept in any template.

Creating navigation menus

We'll begin by creating a new menu, our parent menu, by heading to theNavigation tabin the Shopify Admin, which resides under the欧宝体育官网入口首页linkin the sidebar.

All new stores have a predefined default menu called "Main Menu". To add items to the list, simply click theadd another linkbutton, and give your new item a "link name" and a destination. Theselect drop down将允许您轻松地连接到内部的部分, such as a particular product or collection. Alternatively, you can enter your own URL (either internal or external) by choosing "web address" from the options.

Once we have this in place, we can start to consider the Liquid code we’ll need to output this in our theme.

You can drag and drop nested menu items to create a multi-level navigation, and with some JavaScript and CSS easily style it into a “super-menu” or “drop-down menu”.

Outputting the menu

In order to output the menu in a theme file, we'll need to know the handle of the menu. Handles are unique identifiers within Shopify for products, collections, link lists, and pages.

Let's begin by outputting all the items from theMain Menulink list. We can use a simpleforloop we've used many times before to output the link list items in turn:

The key thing to pay attention to here, is the for-loop that’s been included around each

  • .

    {% forlinkinlinklists.main-menu.links %}

    We are using the variablelinkto hold the data relating to each item in the link list, as we loop over all the items. This keywordlinkcould be anything, it’s just a variable for the for-loop to output data for the menu. In order to accesslinkdata, we need to access all thelinksin the link list with a handle ofmain-menu, hencelinklists.main-menu.links.

    Remember, the defaultMain Menuthat exists in a Shopify store has the handle ofmain-menu, which is why it’s being used above. If our menu had a handle ofsocial-media, the syntax would be refactored as:

    {% for link in linklists.social-media.links %}

    Each link item has properties which include:

    • url
    • title

    In the example above,{{ link.url }}will output the url we entered or generated in the Shopify Admin, and{{ link.title }}will output the link text specific to that url.

    You might also like:How to Manipulate Images with the img_url Filter.

    Multi-level navigation

    Now that we have the basic Liquid structure in place for a single level menu, we need to consider how to create a sub-menu for our top level items. Firstly, we need to head back to the Shopify Admin and create our first sub-menu.

    It might not be 100 percent clear initially, but every link in a link list, in addition to the menu itself, has a unique handle that we have access to in Liquid.

    Let's have a look at an example. If ourmain-menuhas three levels of links as follows:

    • Home
    • About Us
    • Women
      • Accessories
        • Earrings
        • Scarves

    What’s great about using nested menus in Shopify is that nested menu items can be obtained directly from their parent link using Liquid. This greatly simplifies the markup required to render a nested menu — meaning you don’t need to know the handle of the parent to render its children.

    Here's an example of how we can use these related handles to output a three level deep nested menu:

    You'll notice that we’re now introducing anifstatement in our refactored example, directly after we output the first level of our main menu:

    {% if link.links != blank %}

    Thisifstatement checks to see if a child-link for the current link item in our loop exists. If it does exist, the template moves forward and loops over all the items in the sub menu.

    Additionally, in this example we handlechild_linksub-menu and agrandchild_linksub-menu the same way, by checking with anifstatement to see if there’s a child-link for the current link item, and if it does exist, the template loops through and outputs the sub-menu.

    In the example above,child_linkis just aforloop variable we use to represent the current item in the loop; it could easily be replaced withsub_link, andgrandchild_linkwithsub_sub_link. We’ve usedchildandgrandchildin this case to illustrate the hierarchy of the nested navigation a bit more clearly.

    Final touches

    I think it’s important to mention one extra link property that will be very useful when creating menus —link.activeandlink.child_active. These are both boolean properties (true/false) that allow you to easily tell if the current page is active, as well as if it’s nested items are active. The syntax is as follows:

    {% if link.active %} class="active {% if link.child_active %}child-active{% endif %}"{% endif %}

    In this example, we'll add a CSS class ofactiveif the current page URL is the same as the list item, and a class ofactive-childif the current page is also part of the active nested item. Here's the full code example for completeness:

    Start nesting those menus!

    Link lists are a very powerful element of the Shopify platform. Having the ability to create an array of list items that can be changed in the admin gives you lots of flexibility. We've seen theme developers use them far beyond menu structures. However, knowing how to create nested navigation that can then be styled with CSS is a great tool to have at your disposal.

    Grow your business with the Shopify Partner Program

    Whether you offer marketing, customization, or web design and development services, the Shopify Partner Program will set you up for success. Join for free and access revenue share opportunities, tools to grow your business, and a passionate commerce community.

    Sign up
  • Grow your business with the Shopify Partner Program

    Learn more