shopify - How to store value from loop in liquid to display it into the variable
Solution:
You can do something like:
{% assign myTitle = '' %}
{% for collection in product.collections reversed offset:1 %}
{% assign myTitle = collection.title %}
{% break %}
{% endfor %}
{{ myTitle }}
Obviously, it would be a good idea to add some if statements around the assignment to make sure the correct title gets assigned and later on if myTitle is not empty but at minimum, this should do the trick. Remember that this code is going to work well if the product has only one collection but this assumption usually is wrong.
EDIT: optionally you can add a break statement like I did to break it after the first iteration so it won't get reassigned. With this code we assume that the correct title is the title of the first collection in the loop (which could not be true)
Source
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.