I’m trying to retrieve the most recent article on a blog. My current code below does not output anything.
{% for article in blogs['myblog'].articles.last %}
{{ article.title }}
{% endfor %}
I’m trying to retrieve the most recent article on a blog. My current code below does not output anything.
{% for article in blogs['myblog'].articles.last %}
{{ article.title }}
{% endfor %}
2
Answers
It can be done like this using forloop.last:
This assumes that
blogs
is a variable. Otherwise, try replacingblogs['myblog'].articles
withblog.articles
.You don’t need a loop in order to access the last item.
This will set
article
to the last item. You can then use it as expected.Docs: https://shopify.dev/docs/themes/liquid/reference/filters/array-filters#last