本文介绍了Shopify液体获取相关的博客文章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在shopify中,我正在使用液体模板来获取与其产品相关的博客帖子,如下所示:

  < UL> 
{%为blogs.blog.articles%中的文章%}
{%if article.tags包含product.handle%}
< li>< a href ={{article。 url}}>< p> {{article.title}}< / p>< / a>< / li>
{%endif%}
{%endfor%}
< / ul>

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



我的问题是我该怎么做?我曾打算试图将文章放入一个数组中,并测试它是否为空,但是我很难发现它是如何完成的。



谢谢!

解决方案

试试这样:

  {%assign related_posts =%} 

{blog for blogs.blog.articles%中的文章%}
{%if article.tags包含product.handle%}
{%capture post%}
< li>< a href ={{article.url}}>< p> {{article.title}}< / p>< / a> ;< /锂>
{%endcapture%}
{%assign related_posts = related_posts | append:post%}
{%endif%}
{%endfor%}

{%if related_posts.size> 0%}
< ul> {{related_posts}}< / ul>
{%else%}
没有相关帖子!
{%endif%}


In shopify I am using liquid templating to get blog posts which are related to products by their tags, like so:

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

这篇关于Shopify液体获取相关的博客文章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 18:08
查看更多