【问题标题】:Go through array elements in Liquid遍历 Liquid 中的数组元素
【发布时间】:2021-08-12 21:41:46
【问题描述】:

我正在为 shopify 网站编写一些流动代码,以便根据使用产品句柄的标签将产品添加到任何博客文章中。我已经尝试过一种产品,它工作得很好,所以我试图通过一个循环对其进行迭代,但我无法从我创建的数组中获取任何信息。这是我到目前为止写的代码。你能帮我理解我做错了什么吗?谢谢!

{% comment %}declare variables{% endcomment %}
  {% assign related_prod_index = 0 %}
  {% assign related_prod_array = "" | split: ',' %}    
      
{% comment %}check for tags that contains products handles{% endcomment %}
  {% for tag in article.tags %}
      {% if tag contains "product_"%}
        {% assign prod_handle = tag | split:"_" %}
        {% assign blog_prod = all_products[prod_handle[1]] %}
        {% assign related_prod_array = related_prod_array | push:blog_prod  %}
        {% assign related_prod_index = related_prod_index | plus:1 %}
      {% endif %}
  {% endfor%}

{% comment %}check how many product tags I found{% endcomment %} 
 <h1>{{ related_prod_index }} tags found</h1>
     
{% comment %}loop that create small preview for each product{% endcomment %} 
  {% if related_prod_array %} 
      {% for rel_pr in related_prod_array %}
        <img src="{{ rel_pr.featured_image | img_url:'original' }}">
        <p><a href="{{ rel_pr.url}}">{{rel_pr.title}}</a></p>
        <p><a href="{{ rel_pr.url}}">{{rel_pr.price | money}}</a></p>
      {% endfor %}
{% endif %}

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

    这看起来应该可以工作,但如果 this post 为真,那么您不能将 Product 对象添加到数组中。

    好消息是您不需要这样做。将显示代码放在用于搜索标签的同一循环中。

    {% for tag in article.tags %}
       {% if tag contains "product_"%}
            {% assign prod_handle = tag | split:"_" %}
            {% assign blog_prod = all_products[prod_handle[1]] %}
            <img src="{{ blog_prod .featured_image | img_url:'original' }}">
            <p><a href="{{ blog_prod .url}}">{{blog_prod .title}}</a></p>
            <p><a href="{{ blog_prod .url}}">{{blog_prod .price | money}}</a></p>
       {% endif %}
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 2017-09-01
      • 2013-06-23
      • 1970-01-01
      • 2015-05-15
      相关资源
      最近更新 更多