【问题标题】:Shopify liquid get related blog postsShopify Liquid 获取相关博文
【发布时间】:2015-07-14 13:04:27
【问题描述】:

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

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

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

谢谢!

【问题讨论】:

    标签: html shopify liquid templating


    【解决方案1】:

    试试这样的:

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

    【讨论】:

    • 完美,非常感谢 Steph - 现在我对液体有了更多的了解!
    • @Sirrah 没问题 :)
    【解决方案2】:
    <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>
    

    【讨论】:

      猜你喜欢
      • 2013-05-21
      • 1970-01-01
      • 2021-08-09
      • 2022-10-19
      • 2023-02-26
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多