【发布时间】:2021-03-20 10:17:58
【问题描述】:
我正在尝试检索博客上的最新文章。我下面的当前代码没有输出任何东西。
{% for article in blogs['myblog'].articles.last %}
{{ article.title }}
{% endfor %}
【问题讨论】:
我正在尝试检索博客上的最新文章。我下面的当前代码没有输出任何东西。
{% for article in blogs['myblog'].articles.last %}
{{ article.title }}
{% endfor %}
【问题讨论】:
您不需要循环即可访问最后一项。
{% assign article = blogs['myblog'].articles.last %}
这会将article 设置为最后一项。然后您就可以按预期使用它了。
{{ article.title }}
文档:https://shopify.dev/docs/themes/liquid/reference/filters/array-filters#last
【讨论】:
可以这样使用forloop.last:
{% for article in blogs['myblog'].articles %}
{% if forloop.last == true %}
{{ article.title }}
{% endif %}
{% endfor %}
这假定blogs 是一个变量。否则,请尝试将blogs['myblog'].articles 替换为blog.articles。
【讨论】: