在 Shopify 中,如何显示标签列表以及带有该标签的产品数量?
示例:黑色(12)、蓝色(10)。
目前代码看起来像这样,但它不起作用。
<ul>
{% for tag in collection.all_tags %}
<li>
<a href="https://mystore.myshopify.com/collections/all/{{ tag }}">
{{ tag }}
</a>
({{ tag.products_count }})
</li>
{% endfor %}
</ul>
最佳答案
products_count
是 collection
的属性,而不是标签
。
我相信您需要手动循环浏览产品并计算具有指定标签的数量。
例如:
{% assign collection = collections.all %}
<ul>
{% for tag in collection.all_tags %}
{% assign products_count = 0 %}
{% for product in collection.products %}
{% if product.tags contains tag %}
{% assign products_count = products_count | plus: 1 %}
{% endif %}
{% endfor %}
<li>
<a href="https://mystore.myshopify.com/collections/all/{{ tag }}">
{{ tag }}
</a>
({{ products_count }})
</li>
{% endfor %}
</ul>
查看 Shopify 论坛上的这些类似讨论:
关于php - Shopify 标签总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24031787/