如何显示特定集合?

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

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.

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:

确定