首页 > 解决方案 > 生成混合产品和博客文章部分 Shopify

问题描述

我正在尝试制作产品和博客文章的混合部分。我的逻辑如下:

我在合并两个数组时遇到问题。我收到错误消息“参数不是数组元素”。

{% capture product_list %}
  {% for product in collection.products%}
    {{product.title}}|{{product.url}}|{{product.description}}| 
    {{product.featured_image.src | product_img_url: 'medium' }}
    {% if forloop.last == false %}::{% endif%}
  {% endfor %}
{% endcapture %}

{% capture blog_list %}
  {% for blog in blog.articles%}
    {{blog.title}}|{{blog.url}}
    {% if forloop.last == false %}::{% endif%}
  {% endfor %}
{% endcapture %}

{% assign plants = product.list | concat: blog.list %}

{{ plants | join: ", " }}

标签: shopify

解决方案


这取决于您在 Shopify 前端请求方面的位置。如果您在产品页面上,即:/products,Shopify 会确保您拥有有效的产品。在您的情况下,您的代码仅在您迭代产品集合时起作用,因此我们假设您在集合页面上。如果您在博客页面上,您将获得一个博客对象,但没有产品或集合。

所以你的麻烦就来自于此。查看 Shopify中的全局对象,您可以看到您可以使用 all_products 访问任意 20 种产品、任何博客和任何文章,使用它们的句柄。调整你的代码。


推荐阅读