如何在分页中排除集合

  • 本文关键字:排除 集合 分页 shopify
  • 更新时间 :
  • 英文 :


我需要排除分页循环中的集合。但是如果我使用条件语句,它仍然会被计算在分页计数中。

因此,如果我按10分页,并且在第一页中有2个产品来自被排除的集合,那么我将在第一页中只看到8个产品。

有解决方案吗?下面是我的代码片段:

{% paginate collection.products by 10 %}
  <ul>
  {% for product in collection.products %}
    <!-- Check for collection -->
    {% assign is_treatment = false %}
    {% for c in product.collections %}
      {% if c.handle == "salon-treatment" %}
        {% assign is_treatment = true %}
      {% endif %}
    {% endfor %}
    {% unless is_treatment %}
      <li>{{ product.title }}</li>
    {% endunless %}
  {% endfor %}
  </ul>
{% endpaginate %}

注意:这个问题与我在Shopify论坛上发布的问题重复。

对于这样的事情,我首先将所有所需的产品加载到我自己的集合中(基本上是一个对象数组),然后对该数组进行分页…因此,如果您编写一个名为exclude_some_products的函数,返回所有未排除的产品,请这样做:

{% assign my_smaller_collection = exclude_some_products collection %}
{% paginate my_smaller_collection.products by 10 %}
  <ul>
  {% for product in my_smaller_collection.products %}
    <!-- Check for collection -->
    {% assign is_treatment = false %}
    {% for c in product.collections %}
      {% if c.handle == "salon-treatment" %}
        {% assign is_treatment = true %}
      {% endif %}
    {% endfor %}
    {% unless is_treatment %}
      <li>{{ product.title }}</li>
    {% endunless %}
  {% endfor %}
  </ul>
{% endpaginate %}

注。请原谅我的代码,我甚至不确定这是什么语言!

最新更新