How to iterate products in a shopify template

Viewed 188

i'm new to Shopify. I need to create a custom theme for a shop and i'm practicing with loops. But i have a problem with a basic products iteration. in developers guide i saw this

I used :

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

i added this code in index.liquid template, but it doesn't iterate anything. Someone can help me? Thanks

1 Answers

Because collection is a Liquid object that is available only on Collection pages. You can read more at Liquid Objects.

To iterate on index.liquid, you need to use some Global Shopify Object. For example, using the collections object, you can iterate like

{% for product in collections.frontpage.products %}
  {{ product.title }}
{% endfor %}

Frontpage in above code is the name of collection. Replace it with your collection that is on your store and has some products.

Related