Shopify 如何限制要显示的产品数量
我对shopify还不熟悉。我试图限制产品的输出,例如每个标签4个产品。但似乎有一些变量需要我循环。。 有人能帮忙吗?thxShopify 如何限制要显示的产品数量,shopify,Shopify,我对shopify还不熟悉。我试图限制产品的输出,例如每个标签4个产品。但似乎有一些变量需要我循环。。 有人能帮忙吗?thx {% for tag in collection.all_tags %} {% if current_tags contains tag %} <li class="active">{{ tag | link_to_remove_tag: tag }}</li> {% else %} <li
{% for tag in collection.all_tags %}
{% if current_tags contains tag %}
<li class="active">{{ tag | link_to_remove_tag: tag }}</li>
{% else %}
<li>{{ tag | link_to_add_tag: tag }}</li>
{{ tag }}
{% if collection.products.size > 0 %}
<ul class="product-grid just">
{% for product in collection.products %}
{% if product.tags contains tag %}
<li>{% include 'product-grid-item' %}</li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<p><strong><br/>No products found in this collection.</strong></p>
{% endif %}
{% endif %}
{% endfor %}
{%用于集合中的标记。所有_标记%}
{%如果当前_标记包含标记%}
{{tag | link_to_remove_tag:tag}
{%else%}
{{tag | link_to_add_tag:tag}
{{tag}}
{%如果collection.products.size>0%}
{%用于集合中的产品。产品%}
{%如果product.tags包含标记%}
- {%include'产品网格项“%”
{%endif%}
{%endfor%}
{%else%}
此系列中未找到任何产品。
{%endif%}
{%endif%}
{%endfor%}
为了实现良好的分离,最好将要显示的项目数逻辑放在“列表”的某个位置,以便输出
这样,您就不必在视图中进行与内容相关的数学运算,这绝不是一个好主意,因为它不会将代码放在它所属的位置,其他想要更改内容的人可能很难找到它。也就是说,您可以在将对象交给渲染之前,根据配置的后端参数对collection.products对象进行限制,或者进一步返回并在“从数据库获取产品”点进行干预,并限制/更改结果
当然有可能使用递增计数器并在模板中的4处停止,但我很难推荐使用它,因为它不是逻辑应该位于的位置。模板应该显示,而不是考虑要显示什么,否则很难重复使用。可以用于显示集合中的前4个产品:
{% for product in collection.products limit:4 %}
但是,如果需要使用给定的标签显示有限数量的产品,则需要实现自己的计数器。在Shopify论坛上,可以看到一个如何实现这一点的示例
例如
{%用于集合中的标记。所有_标记%}
{{tag}}
{%assign counter=0%}
{%用于集合中的产品。产品%}
{%如果product.tags包含标记和计数器<4%}
{%include'产品网格项“%”
{%分配计数器=计数器|加:1%}
{%endif%}
{%endfor%}
{%endfor%}
它是关于信息结构的:产品、产品列表、集合等都是商店内容的一部分。它不是视图逻辑的一部分。它不应该在模板中。转到生成集合对象的位置,并更改产品以显示在那里。模板不应该知道它显示了什么,而应该知道如何显示。
{% for tag in collection.all_tags %}
{{ tag }}
{% assign counter = 0 %}
{% for product in collection.products %}
{% if product.tags contains tag and counter < 4 %}
<li>{% include 'product-grid-item' %}</li>
{% assign counter = counter | plus: 1 %}
{% endif %}
{% endfor %}
{% endfor %}