You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Shopify区块中遍历并展示collection_list内的集合?

解决方案:遍历区块内的collection_list集合列表

要展示ID为sub-subcategory-collection_list的集合列表中的所有集合,直接修改模板里的循环逻辑即可,以下是修改后的完整代码:

<!-- If The Category Has So Many Subs -->
<div class="sub-cat-childs-container">
{% for block in section.blocks %}
{% case block.type %}
{% when 'Sub-Category' %}
{% if shop.locale == 'en' %}<style>.subCategory::after{float: right}</style>{% endif %}
  <div class="sub--category--contnet" id="sub-category-{{ block.settings.sub_category_collection.id }}">
  <div class="sub-sub---items-row">
    {% comment %} 遍历集合列表 {% endcomment %}
    {% for collection in block.settings.sub-subcategory-collection_list %}
      <div class="sub-sub-category-item">
        <a href="{{ collection.url }}">
          {% if collection.image %}
            <img src="{{ collection.image | img_url: 'medium' }}" alt="{{ collection.title }}">
          {% endif %}
          <h3>{{ collection.title }}</h3>
        </a>
      </div>
    {% endfor %}
  </div>
 </div>
{% endcase %}
{% endfor %}
</div>
{% endif %}

关键说明:

  • 核心逻辑是用{% for collection in block.settings.sub-subcategory-collection_list %}循环遍历集合列表,这是Shopify中访问collection_list类型设置的标准方式
  • 循环内部可根据需求调用集合的各类属性:
    • collection.title:获取集合名称
    • collection.url:获取集合页面链接
    • collection.image:获取集合封面图(配合img_url过滤器可设置图片尺寸)
  • 你可以根据页面样式需求,调整.sub-sub-category-item内部的HTML结构

内容的提问来源于stack exchange,提问作者Mohamed Hamis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 19:09:31