我正在遍历我的首页产品集合并生成一个表格。在最后一行,我需要在第一个表格单元格中放置一些静态内容。我已经能够做到这一点,但由于某种原因,循环在它这样做之后停止了。有谁知道为什么吗?
{% tablerow product in collections.frontpage.products cols: 2 %}
{% if tablerowloop.col_first and tablerowloop.last %}
<img src="{{'box.png' | asset_url}}">
{% else %}
<div class='featured-product'>
<img src="{{ product.images[1] | product_img_url: 'medium' }}">
<p>{{ product.title }}</p>
</div>
{% endif %}
{% endtablerow %}
最佳答案
tablerowloop.last
只为表的最后一个单元格(即集合的最后一个产品)返回 true,而不是最后一行。相反,您需要检查索引是否在集合末尾的 cols
内,如果它是第一列,则它必须是最后一行。
{% tablerow product in collections.frontpage.products cols: 2 %}
{% assign x = tablerowloop.length | minus:2 %}
{% if tablerowloop.col_first and tablerowloop.index > x %}
<img src="{{'box.png' | asset_url}}">
{% else %}
<div class='featured-product'>
<img src="{{ product.images[1] | product_img_url: 'medium' }}">
<p>{{ product.title }}</p>
</div>
{% endif %}
{% endtablerow %}
关于html - Shopify tablerow 循环提前退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13186336/