skip to Main Content

I have create many collections like All Products, New Releases, T-Shirts, Pants etc. And I have two categories of products like Men’s and Women’s. Some collections specifically for Men’s and some collections specifically for Women’s. I create two tags Men’s and Women’s. I create menu by collections query by tags. My my products collection url is like this:

/collections/all-products/mens
/collections/all-products/womens
/collections/new-releases/mens
/collections/new-releases/womens
/collections/bras/womens

I want to show some text and menu list when collection.url show /mens or /womens.

{% if collection.url contains mens %}
    do something
{% endif %

Above condition not working. I know why not working because {{ collection.url }} provide /collections/all-products. {{ page.url }} will not work for collection object. I haven’t find any suggestion or liquid code reference where show men’s or women’s products in collections page it will show specific text.

If use in loop it will work.

{% for product in collection.products %}
    {% for tag in product.tags %}
        {% if tag contains 'mens' %}
            <h3>Mens Products</h3>
        {% endif %}
    {% endfor %}
{% endfor %}

Above code will not work for me because it’s inside loop. I need to show outside of loop. I don’t understand how to achieve it. here is the reference site. Below have image how I want.

enter image description here

Need help!

2

Answers


  1. You have access to the current_tags, refer to docs: https://shopify.dev/docs/themes/liquid/reference/objects/current-tags

    This will return an array of all the tags you are viewing at the moment (in case you are viewing more than one tag).

    So your check will be:

    {% if current_tags contains 'mens' %}
        do something
    {% endif %}
    

    That’s pretty much the just of it.

    Login or Signup to reply.
  2. I really like how the menu is coming along! Here are some ideas you could consider for your category specific menu, if you’ve not gotten where you want yet.

    1. For your url strategy, what you’re looking for is handle. Handles are specific to liquid. https://shopify.dev/docs/themes/liquid/reference/basics/handle

    2. You could make a custom collection template if those 2 categories need to be fairly different: https://shopify.dev/tutorials/customize-theme-create-alternate-templates. If you do that, then you can use template_prefix from the collection object.

    3. Assign a variable outside your loop and then set it inside the loop like:

      {% assign is_mens = false %}
      {% for tag in product.tags %}
      {% if tag contains 'mens' %}
      {% assign is_mens = true %}
      {% endif %}
      {% endfor %}

    then {% if is_mens %} or {% unless is_mens %} for your dynamic content, or a case statement to define content specific to categories in your menu.

    Hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search