Shopify liquid获取相关博客文章

时间:2015-05-05 00:03:05

标签: html shopify liquid templating

在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>

但是,如果没有相关帖子,我想显示一条消息,例如&#34;没有相关帖子!&#34;

我的问题是我该怎么做?我已经打算尝试将文章放入数组并测试它是否为空,但我很难找到如何完成。

谢谢!

2 个答案:

答案 0 :(得分:4)

尝试这样的事情:

{% 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 %}

答案 1 :(得分:0)

id
相关问题