Looping through collections in section

corywatilo
New Member
23 0 0

I'm creating multiple sliders on a single page by allowing the user to include multiple collections. Works great with one collection, but when I add a 2nd, liquid doesn't know what to do with it.

I'm thinking I'm just missing some looping instruction - like to tell it to loop through each collection selected by the user in the theme editor.

  {% for block in section.blocks %}

    {{ collection.title }}
    <ul class="imageGallery">
      {% for product in collection.products %}

        <li data-thumb="{{ product.images.last | img_url: '1024x1024' }}" data-src="{{ product.images.last | img_url: '1024x1024' }}" data-sub-html="<h4>{{ product.title }}</h4><p>{{ product.description | strip_html | truncate: 100 }} <a href='{{ product.url | within: collection }}'>view product</a></p>">
          <img src="{{ product.images.last | img_url: '1024x1024' }}" />
        </li>
      {% endfor %}
    </ul>
  {% endfor %}

  {% schema %}
    {
      "name": "Collection list",
      "max_blocks": 12,
      "settings": [
        {
          "type": "text",
          "id": "title",
          "label": "Heading",
          "default": "Collection list"
        }
      ],
      "blocks": [
        {
          "type": "collection",
          "name": "Collection",
          "settings": [
            {
              "label": "Collection",
              "id": "collection",
              "type": "collection"
            }
          ]
        }
      ]
    }
  {% endschema %}

Currently with this code, it works with one collection, but adding a second seems to limit the page to only pulling content from the last collection. What am I missing?

Replies 2 (2)
corywatilo
New Member
23 0 0

Doh! Got it:

    {%- assign collection = collections[block.settings.collection] -%}

 

zurain
Excursionist
24 0 5

@corywatilo bro plz give me complete code