2017-05-04 46 views
0

我想要完成的是当用户在单独的博客文章/文章中,我想根据匹配标签显示独特的“相关文章”。Shopify - 根据当前文章的标签获取独特的文章

这是我到目前为止有:

{% for tag in article.tags %} 

    {% assign counter = 0 %} 
    {% for article in blog.articles %} 
    {% if article.tags contains tag and counter < 2 %} 
     {% assign counter = counter | plus: 1 %} 
     <li class="well"> 
     {% if article.excerpt.size > 0 %} 
      <div class="thumb-article"> 
       <a href="{{ article.url }}"> 
        {{ article.excerpt }} 
       </a> 
      </div> 
      {% endif %} 
       <h3><a href="{{ article.url }}">{{ article.title }}</a></h3> 
       <p>{{ article.content | strip_html | strip_newlines | truncatewords: 40 }}</p> 
     </li> 
    {% endif %} 
    {% endfor %} 

{% endfor %} 

出人意料的是,(对我来说,因为这是我与Shopify和液体第一次的经验)它的工作原理,只是有点太清楚,因为它得到重复的帖子。

有没有什么方法可以防止它获得重复的文章?

+0

您的链接已锁定密码,因此我们无法看到它。 –

+1

此代码可以帮助您吗? http://stackoverflow.com/questions/30042090/shopify-liquid-get-related-blog-posts –

+0

或者这个:https://apps.shopify.com/related-blog-posts –

回答

0

这个讨论有你需要的东西:Shopify liquid get related blog posts

它创建变量一个空相关的帖子,然后它定义在一个循环中,看起来通过同一类别的其他职位。重复它的答案:

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

转到上面的链接,以查看完整的响应。