shopify - 生成混合产品和博客文章部分 Shopify
问题描述
我正在尝试制作产品和博客文章的混合部分。我的逻辑如下:
- 获取所有产品的数组
- 获取包含所有博客文章的数组
- 按照模式合并两个数组(3 个产品 - 2 个帖子)
- 显示数据
我在合并两个数组时遇到问题。我收到错误消息“参数不是数组元素”。
{% 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 前端请求方面的位置。如果您在产品页面上,即:/products,Shopify 会确保您拥有有效的产品。在您的情况下,您的代码仅在您迭代产品集合时起作用,因此我们假设您在集合页面上。如果您在博客页面上,您将获得一个博客对象,但没有产品或集合。
所以你的麻烦就来自于此。查看 Shopify中的全局对象,您可以看到您可以使用 all_products 访问任意 20 种产品、任何博客和任何文章,使用它们的句柄。调整你的代码。
推荐阅读
- python - 简单插补 - 返回值 Err
- python - 如何使用其值对字典进行排名
- spring - 无法将“java.lang.String”类型的值转换为所需的“java.util.Date”类型
- python - Django ORM SQL 原始 Mysql
- javascript - 全局暴露了一个 es6 模块但为空
- php - 使用 php 解析 SOAP API
- react-native - React-Native textInput 添加数字而不是添加数字
- reactjs - 使用 axios 向 elasticsearch 发送请求以映射卡片
- python - Discord Bot 上的循环功能不起作用
- powerbi - 计算连续行之间的百分比差异 - Power BI