0
votes

I have a menu (linklists.all-collections) that contains a list of collections. The menu outputs the collection title, url and also any tags associated with the collection using collection.all_tags.

The menu outputs fine, however the next time I call collection.title on a collection template page it outputs the last collection title in the menu instead of the collection title of the current collection page.

Is there a way to close the loop (similar to how WordPress wp_reset_query() destroys the previous query and sets up a new query)?

Menu is:

<ul>
    {% for link in linklists.all-collections.links %}
    {% assign collection = link.object %}
    <li class="sidebar-menu-item {{ collection.handle }}">
       <a href="{{ collection.url }}" title="{{ 'collections.general.link_title' | t: title: collection_title }}">{{ collection.title }}</a>
         <ul>
         {% for tag in collection.all_tags %} 
           {% unless tag == 'exclude' %} 
             <li>
               <a href="{{ shop.url }}/collections/{{ collection.handle }}/{{ tag }}">{{ tag }}</a>  
             </li>
           {% endunless %}
        {% endfor %}
         </ul>
     </li>
    {% endfor %}
</ul>
1

1 Answers

2
votes

The issue is caused by this line :

{% assign collection = link.object %}

By the way, you do not need to do that to access your collection attributes : https://help.shopify.com/themes/liquid/objects/link#link-object

Something like this should work (not tested):

<ul>
    {% for link in linklists.all-collections.links %}
    <!-- First check if your link is collection -->
    {% if link.type == 'collection_link' %}
    <li class="sidebar-menu-item {{ link.object.handle }}">
       <a href="{{ link.url }}" title="{{ 'collections.general.link_title' | t: title: collection_title }}">{{ link.object.title }}</a>
         <ul>
         {% for tag in link.object.all_tags %} 
           {% unless tag == 'exclude' %} 
             <li>
               <a href="{{ shop.url }}/collections/{{ link.object.handle }}/{{ tag | handleize }}">{{ tag }}</a>  
             </li>
           {% else %}
           <!-- If condition not met continue to next iteration -->
           {% continue %}
           {% endunless %}
        {% endfor %}
         </ul>
     </li>
    {% else %}
    <!-- If not collection link, continue to next iteration in the loop -->
    {% continue %}
    {% endif %}
    {% endfor %}
</ul>