液体模板:for 循环中的偶数/奇数项
如果我在 Liquid 中有一个 for 循环(使用 Jekyll),我如何才能只定位偶数(或奇数)项?我已经尝试过:
{% for item in site.posts %}
{% if forloop.index % 2 == 1 %}
但这似乎不起作用。我也尝试过:
(forloop.index % 2) == 1
和:
forloop.index - (forloop.index / 2 * 2) == 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你会想为此使用循环标签。例如:
如果您希望每个周期使用不同的 HTML 标记:
您可以在 Liquid for Designers,尽管那里的示例并不是特别有帮助。此 Shopify 支持线程也应该有所帮助。
I think you'll want to use the cycle tag for this. For example:
If you want different HTML markup for each cycle:
You can find more information on it at Liquid for Designers, although the example there isn't particularly helpful. This Shopify support thread should also help.
与 Shopify 支持线程相反/stackoverflow.com/a/8980590/6884">Ales Lande 的回答说,Liquid中有一个
modulo
函数 - 形式为modulo
过滤器。有了它,您可以执行以下操作:
In contrast to what the Shopify support thread in Ales Lande's answer says, there is a
modulo
function in Liquid - in form of themodulo
filter.With it, you can do this: