首页 > 解决方案 > Liquid 错误:数组“collection.products”不可分页

问题描述

我在section中创建了一个组件并写了名字bottom.liquid并从collection-template复制的代码都具有相同的架构并在section文件夹中创建但我只在bottom.liquid中出现错误在此处输入图像描述

这是我的bottom.liquid代码

{% paginate collection.products by 12 %}
  
  {% assign productCount = collection.all_products_count | minus: paginate.current_offset %}

  <div class=" container-fluid  lemon-con ">
        
        <div class="column-title">
          <h3 class="h3 main-lemo-heading text-center">
            DIRTYLEMON
        </h3>
        </div>
      
        <div class="row">
            <div class="col-md">
                {% if section.settings.collection_nav %}

                  {% assign sidebarNav = section.settings.collection_nav %}

                  <div class="list-card mb-4">
                      <div class="card-header product-hidden">
                        <strong>{{ linklists[sidebarNav].title }}</strong>
                      </div>
                      <ul class="list-group list-group-flush">
                        
                          {% for link in linklists[sidebarNav].links %}
                             <li class="list-group-itm">
                               <a  href="{{ link.url }}">{{ link.title }}</a>

                             </li>
                          {% endfor %}
                      </ul>
                      

                  </div>
                  
                {% endif %}

                {% if collection.all_tags.size > 0 and section.settings.hide_tags != true %}
                  <div class="list-card mb-4">
                      <div>
                        <strong>Tags</strong>
                      </div>

                      <ul class="list-group list-group-flush">
                        {% for tag in collection.all_tags %}
                          {% if current_tags contains tag %}
                            <li class="list-group-itm bg-primary">
                              {{ tag | link_to_remove_tag: tag }}
                            </li>
                          {% else %}
                            <li class="list-group-itm side-bar">
                              {{ tag | link_to_tag: tag }}
                            </li>
                          {% endif %}
                        {% endfor %}
                      </ul>

                  </div>
                {% endif %}

            </div>

      

        </div>



  </div>

  
{% endpaginate %}



{% schema %}
  {
    "name": "bottom",
    "settings": [
      {
        "type": "header",
        "content": "Collection header"
      },
      {
        "type": "text",
        "id": "title",
        "label": "Heading",
        "default": "slider title"
    },
      {
        "type": "checkbox",
        "id": "is_full_width",
        "label": "Full width",
        "default": false
      },
      {
        "type": "select",
        "id": "header_align",
        "options": [
            { 
              "value": "right", 
              "label": "Text right"
            },
            { 
              "value": "center", 
              "label": "Text center"
            },
            { 
              "value": "left", 
              "label": "Text left"
            }
        ],
        "label": "Header alignment",
        "default": "left"
      },
      {
        "type":      "range",
        "id":        "header_height",
        "min":       50,
        "max":        500,
        "step":       5,
        "unit":       "px",
        "label":     "Header height",
        "default":   120
      },
      {
        "type": "header",
        "content": "Sidebar"
      },
      {
        "type": "link_list",
        "id": "collection_nav",
        "label": "Navigation",
        "info": "Select custom menu nav for sidebar"
      },
      {
        "type": "checkbox",
        "id": "hide_tags",
        "label": "Hide tags",
        "default": false,
        "info": "Hide tags from sidebar"
      },
      {
        "type": "header",
        "content": "Others"
      },
      {
        "type": "paragraph",
        "content": "You can add more settings here :) "
      }
    ],
    "presets": [
        {
            "name": "bottom",
            "category": "Image"
        }
    ]
  
  }
{% endschema %}

两者都有相同的代码,但我在bottom.liquid的bottom.liquid中遇到错误我在上面的图像collection.template上复制了相同的代码,但我只在bottom.liquid中遇到错误 在此处输入图像描述

标签: shopifyliquid

解决方案


对于为什么它不起作用,Shopify Docs for Global objects没有提到 collection 作为全局对象。因此,您的代码仅适用于 Liquid 中的集合对象指的是正在查看的集合的集合页面。

所以你可以做的是,使用集合全局对象,然后使用集合句柄对所需的集合进行分页。

{% paginate collections['my-handle'].products by 12 %}
{% endpaginate %}

如果您想使用主题定制器选择 Collection,您可以使用Collection的设置类型。

{
   "type": "collection",
   "id": "feature_collection",
   "label": "Feature collection"
}

推荐阅读