Shopify get nav handle from a link

Viewed 805

Is that possible to get nav's handle or name from a Url inside that nav?

Let say i have a Url of a collection https://avasahome.myshopify.com/collections/ABC that is inside a ABC Test nav which the handle is abc-test.

Can i get abc-test from the Url https://avasahome.myshopify.com/collections/ABC?

Please help me with this cause i found no document about this matter.

Thank you!

2 Answers

Put the below code in your liquid file. This is basically a function which invoked will return you the Nav Link Text

get_nav = function(){
  var i, len, nav, ref;

  ref = $('.nav__link');
  for (i = 0, len = ref.length; i < len; i++) {
    nav = ref[i];
    if (nav.href === document.location.href) {
      alert(nav.text);
      //return nav.text;
    }
  }
}

This will assign your desired value as retrievedLinkHandle:

{%- assign retrievedLinkHandle = false -%}
{%- for link in nav.links -%}
  {%- if link.type == 'collection_link' and link.object.id == collection.id -%}
    {%- assign retrievedLinkHandle = link.title | handle -%}
    {%- break -%}
  {%- endif -%}  
{%- endfor -%}
Related