英文:
How can I display specific collection?
问题
<h2>{{ section.settings.title }}</h2>
{%- if section.settings.description != blank or section.settings.show_description and section.settings.collection.description != empty -%}
<div>
{%- if section.settings.show_description -%}
{{ section.settings.collection.description }}
{%- else -%}
{{ section.settings.description -}}
{%- endif %}
</div>
{%- endif -%}
{% assign featuredCollection = collections['Featured Product'] %}
{% if featuredCollection %}
<h2>{{ featuredCollection.title }}</h2>
{% unless featuredCollection.description == blank %}
<p>{{ featuredCollection.description }}</p>
{% endunless %}
{% for product in featuredCollection.products %}
{% if product.image %}
<img
src="{{ product | img_url }}">
{% endif %}
<h3>{{ product.title }}</h3>
<p>{{ product.price | money }}</p>
{% else %}
<p>There is no product in {{ featuredCollection.title }}</p>
{% endfor %}
{% else %}
<p>Featured Product collection not found</p>
{% endif %}
英文:
For the first part I was able to get and display the name and description of the collection by using this code:
<h2>{{ section.settings.title }}</h2>
{%- if section.settings.description != blank or section.settings.show_description and section.settings.collection.description != empty -%}
<div>
{%- if section.settings.show_description -%}
{{ section.settings.collection.description }}
{%- else -%}
{{ section.settings.description -}}
{%- endif %}
</div>
{%- 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:
{% for collection in collections %}
<h2>{{ collection.title }}</h2>
{% unless collection.description == blank %}
<p>{{ collection.description }}</p>
{% endunless %}
{% for product in collection.products %}
{% if product.image %}
<img
src="{{ product | img_url }}">
{% endif %}
<h3>{{ product.title }}</h3>
<p>{{ product.price | money }}</p>
{% else %}
<p>There is no product in {{ collection.title }}</p>
{% endfor %}
{% 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
用于获取所选集合中的产品。
{%- for product in section.settings.collection.products -%}
// 使用 product 在每个产品的循环中显示所需数据
{%- endfor -%}
请告诉我,这是否对您有帮助。
英文:
I assune you using the following collection input to Shopify theme.
using the following schema
{
"type": "collection",
"id": "collection",
"label": "Collection"
}
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.
{%- for product in section.settings.collection.products -%}
// use product to display desired data inside loop for each product
{%- endfor -%}
Let me know, if this will helps you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论