如何显示特定集合?

huangapple go评论109阅读模式
英文:

How can I display specific collection?

问题

  1. <h2>{{ section.settings.title }}</h2>
  2. {%- if section.settings.description != blank or section.settings.show_description and section.settings.collection.description != empty -%}
  3. <div>
  4. {%- if section.settings.show_description -%}
  5. {{ section.settings.collection.description }}
  6. {%- else -%}
  7. {{ section.settings.description -}}
  8. {%- endif %}
  9. </div>
  10. {%- endif -%}
  11. {% assign featuredCollection = collections['Featured Product'] %}
  12. {% if featuredCollection %}
  13. <h2>{{ featuredCollection.title }}</h2>
  14. {% unless featuredCollection.description == blank %}
  15. <p>{{ featuredCollection.description }}</p>
  16. {% endunless %}
  17. {% for product in featuredCollection.products %}
  18. {% if product.image %}
  19. <img
  20. src="{{ product | img_url }}">
  21. {% endif %}
  22. <h3>{{ product.title }}</h3>
  23. <p>{{ product.price | money }}</p>
  24. {% else %}
  25. <p>There is no product in {{ featuredCollection.title }}</p>
  26. {% endfor %}
  27. {% else %}
  28. <p>Featured Product collection not found</p>
  29. {% endif %}
英文:

For the first part I was able to get and display the name and description of the collection by using this code:

  1. <h2>{{ section.settings.title }}</h2>
  2. {%- if section.settings.description != blank or section.settings.show_description and section.settings.collection.description != empty -%}
  3. <div>
  4. {%- if section.settings.show_description -%}
  5. {{ section.settings.collection.description }}
  6. {%- else -%}
  7. {{ section.settings.description -}}
  8. {%- endif %}
  9. </div>
  10. {%- endif -%}

and I am trying to display the selected collection that I choose in customizer which the the "Featured Collection" and be able to display it in my page. But with this code:

  1. {% for collection in collections %}
  2. <h2>{{ collection.title }}</h2>
  3. {% unless collection.description == blank %}
  4. <p>{{ collection.description }}</p>
  5. {% endunless %}
  6. {% for product in collection.products %}
  7. {% if product.image %}
  8. <img
  9. src="{{ product | img_url }}">
  10. {% endif %}
  11. <h3>{{ product.title }}</h3>
  12. <p>{{ product.price | money }}</p>
  13. {% else %}
  14. <p>There is no product in {{ collection.title }}</p>
  15. {% endfor %}
  16. {% endfor %}

It display all collections that I created such a For Men, For Women, etc. But I only want to display the "Featured Product" collection. How can I display the "Featured Product" only?

答案1

得分: 0

I assume you are using the following collection input in your Shopify theme.

使用以下模式来获取相同的内容到Liquid代码中,因为它返回了集合对象。

section.settings.collection.products 用于获取所选集合中的产品。

  1. {%- for product in section.settings.collection.products -%}
  2. // 使用 product 在每个产品的循环中显示所需数据
  3. {%- endfor -%}

请告诉我,这是否对您有帮助。

英文:

I assune you using the following collection input to Shopify theme.

如何显示特定集合?

using the following schema

  1. {
  2. "type": "collection",
  3. "id": "collection",
  4. "label": "Collection"
  5. }

To get the same into liquid code you use directly like this way, beacuase it return the collection object.

如何显示特定集合?

section.settings.collection.products to get the products from selected collection.

  1. {%- for product in section.settings.collection.products -%}
  2. // use product to display desired data inside loop for each product
  3. {%- endfor -%}
  4. Let me know, if this will helps you.

huangapple
  • 本文由 发表于 2023年5月14日 02:43:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定