获取上一篇文章-Shopify/Liquid
我正在尝试检索博客上的最新文章。我下面的当前代码不输出任何内容。
{% 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
- This should be the accepted answer. My understanding is unnecessary "for" loops in liquid should be avoided as they take longer to be processed than a filter.