如何将产品从循环中分离出来并在shopify中将它们按2分组
how to separate products from loop and put them in to division group by 2 in shopify
我有一个产品数组 {1,2,3,4,5,6,7,8}
我想把它们分成 div 组
我有这样的结构 问题是我将所有产品合二为一 div 我想分开每两个产品
<div class="images">
images 1 to 8
</div>
我需要这样的结构
<div class="images">
images 1 and 2
图片 3 和 4
图片 5 和 6
图片 7 和 8
{% for media in product.media %}
<div class="image-block ">
<a href="{{ media.preview_image | img_url: product_image_zoom_size, scale: product_image_scale }}"
class=" product-single__thumbnail--{{ section.id }} even count-{{forloop.index}} "
data-thumbnail-id="{{ section.id }}-{{ media.id }}"> <img class="product-single__thumbnail-image" src="{{ media.preview_image | img_url: 'large', scale: 2 }}" alt="{{ thumbnailAlt }}"></a>
</div>
{% endfor %}
在你的循环中使用逻辑来打开和关闭以及根据你的需要 HMTL
元素。
{% for media in product.media %}
{% assign data = forloop.index | modulo:2 %}
{% if data != 0 %}<div class="wrapper">{% endif %}
inner content goes here
{% if data == 0 %}</div>{% endif %}
{% endfor %}
这是相同代码的工作示例:
我有一个产品数组 {1,2,3,4,5,6,7,8}
我想把它们分成 div 组
我有这样的结构 问题是我将所有产品合二为一 div 我想分开每两个产品
<div class="images">
images 1 to 8
</div>
我需要这样的结构
<div class="images">
images 1 and 2
图片 3 和 4
图片 5 和 6
图片 7 和 8
{% for media in product.media %}
<div class="image-block ">
<a href="{{ media.preview_image | img_url: product_image_zoom_size, scale: product_image_scale }}"
class=" product-single__thumbnail--{{ section.id }} even count-{{forloop.index}} "
data-thumbnail-id="{{ section.id }}-{{ media.id }}"> <img class="product-single__thumbnail-image" src="{{ media.preview_image | img_url: 'large', scale: 2 }}" alt="{{ thumbnailAlt }}"></a>
</div>
{% endfor %}
在你的循环中使用逻辑来打开和关闭以及根据你的需要 HMTL
元素。
{% for media in product.media %}
{% assign data = forloop.index | modulo:2 %}
{% if data != 0 %}<div class="wrapper">{% endif %}
inner content goes here
{% if data == 0 %}</div>{% endif %}
{% endfor %}
这是相同代码的工作示例: