Return the first collection that a product belongs to?

Shopify Partner
22 0 1

I have some products which belong to multiple collections, say 'Frontpage' and 'Beef' --

Is there a way to only return the first collection, say I have a loop which returns the collection names:

{% for collection in product.collections %}
   {{ collection.title }}
{% endfor %}


Returns 'Frontpage Beef'

Is there a way to limit this to the first in the collection? At the moment i'm having to use something like this:

 

| replace: 'Frontpage', ''

But this is obviously a bit hacky and I have to keep adding `replace: 'collectionname'` new collections are added.

0 Likes
Astronaut
1890 1 393

I'm not sure if this is what you're looking for.

{{ product.collections[0].title }}

Remember that this is using a zero-index.  "0" is the first entry, "1" is the second, etc.

I don't think that the forloop is needed if only doing one iteration.  In case that the forloop is needed, you can take a look into the limit and offset parameters.

I hope this helps.

1 Like
Shopify Partner
22 0 1

Hey Alex,

Thanks - perfect!

Cheers,

0 Likes
Explorer
70 3 8

Hey,

 

By 1st does it mean latest or 1st one added to?

 

Is there a way to specify one a primary 1st added to?

 

Thanks

0 Likes