Ad

Directly Access Nested Linklist In Shopify

- 1 answer

I have a my menu navigation set up like this in shopify:

- main_menu
    - top_level_1
    - top_level_2
        - top_level_2_sub_menu_1
        - top_level_2_sub_menu_2
        - top_level_2_sub_menu_3

I am able to do this to iterate through all the menu items:

    {% for link in linklists.main-menu.links %}
        <p>{{ link.handle }}</p>
        {% for nestedLink in link.links %}
            <p>{{ nestedLink.handle }}</p>
        {% endfor %}
    {% endfor %}

However I don't seem to be able to iterate JUST through the submenus belonging to top_level_2. I have tried this with no success:

{% for link in linklists.main-menu.top_level_2.links %}
    <p>{{ link.handle }}</p>
{% endfor %}

And also tried this:

{% for link in linklists.top_level_2.links %}
    <p>{{ link.handle }}</p>
{% endfor %}

Is it possible to directly get a handle to linklist in order to iterate through its child links? Or would I need to first find that link by searching the top level items?

Ad

Answer

linklists.main-menu.links returns an array of object items.

A.k.a:

[
  {
    active: false
    child_active: false
    current: false
    child_current: false
    levels: 1
    links: [...]
    object: null
    title: "Some title"
    type: "http_link"
    url: "#:
  },
  {
    active: false
    child_active: false
    current: false
    child_current: false
    levels: 1
    links: [...]
    object: null
    title: "Some title"
    type: "http_link"
    url: "#:
  },
  {
    active: false
    child_active: false
    current: false
    child_current: false
    levels: 1
    links: [...]
    object: null
    title: "Some title"
    type: "http_link"
    url: "#:
  },
  etc...
]

So you can't say linklists.main-menu.top_level_2 since there is no such object.

You can say linklists.main-menu.links[1].links and do something like so:

{%- for link in linklists.main-menu.links[1].links -%}

{%- endfor -%}

But please note that this will loop only the items in the second navigation item and ignore the rest.

Ad
source: stackoverflow.com
Ad