Shopify liquid 获取相关博文

Shopify liquid get related blog posts

在 shopify 中,我使用液体模板来获取通过标签与产品相关的博客文章,如下所示:

<ul>
{% for article in blogs.blog.articles %}
    {% if article.tags contains product.handle %}
        <li><a href="{{ article.url }}"><p>{{ article.title }}</p></a></li>
    {% endif %}
{% endfor %}
</ul>

但是,如果没有相关帖子,我想显示一条消息,例如"No related posts!"

我的问题是我该怎么做?我曾考虑尝试将文章放入一个数组中并测试它是否为空,但我很难弄清楚这是如何完成的。

谢谢!

<ul>
    {% for article in blogs.blog.articles %}
    {% if article.tags contains product.handle %}
        <li><a href="{{ article.url }}"><p>{{ article.title }}</p></a></li>
    {% else %}
        <li>No related blog posts!</li>
    {% endif %}
    {% endfor %}
</ul>

尝试这样的事情:

{% assign related_posts = "" %}

{% for article in blogs.blog.articles %}
  {% if article.tags contains product.handle %}
    {% capture post %}
      <li><a href="{{ article.url }}"><p>{{ article.title }}</p></a></li>
    {% endcapture %}
    {% assign related_posts = related_posts | append:post %}
  {% endif %}
{% endfor %}

{% if related_posts.size > 0 %}
  <ul> {{ related_posts }} </ul>
{% else %}
  No related posts!
{% endif %}