简体   繁体   English

Shopify Liquid获取相关博客文章

[英]Shopify liquid get related blog posts

In shopify I am using liquid templating to get blog posts which are related to products by their tags, like so: 在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>

However, if there are no related posts, I would like to display a message such as "No related posts!" 但是,如果没有相关信息,我想显示一条消息,例如“无相关信息!”。

My question is how would I do that? 我的问题是我该怎么做? I have contemplated trying to get the articles into an array and testing if it is empty, but I am having difficulty finding out how this is done. 我已经考虑过尝试将文章整理成一个数组并测试它是否为空,但是我很难找出这样做的方式。

Thanks! 谢谢!

Try something like this: 尝试这样的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM