我想要完成的是当用户访问个人博客文章/帖子时,我想根据匹配的标签显示独特的“相关文章”。
这是我到目前为止所拥有的:
{% 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 :(得分:0)
此主题符合您的要求:Shopify liquid get related blog posts
它创建一个空的相关posts变量,然后在循环中定义它,查看同一类别的其他帖子。重复一遍:
com.amazonaws.SdkClientException: Unable to find a region via the region provider chain.
Must provide an explicit region in the builder or setup environment to supply a region.
转到上面的链接,查看完整的回复。
答案 1 :(得分:0)
对于那些也在搜索如何将相关博客文章实施到博客(而不是产品)(如提供的链接)中的那些文章的人,上面的代码已修复:
...
{% assign skip_articles = article.handle | split: '.....' %}
...
{% for ...
{% if ...
{% unless skip_articles contains related_article.handle %}
...
{% assign temp = related_article.handle | split: '.....' %}
{% assign skip_articles = skip_articles | concat: temp %}
...
被在句柄中找不到的任何东西分割以创建数组
最后得到这样的东西:
<div class='relatedArticles'>
{% for tag in article.tags %}
{% assign counter = 0 %}
{% assign skip_articles = article.handle | split: '.....' %}
{% for related_article in blog.articles %}
{% if related_article.tags contains tag and counter < 6 %}
{% unless skip_articles contains related_article.handle %}
{% assign counter = counter | plus: 1 %}
{% assign temp = related_article.handle | split: '.....' %}
{% assign skip_articles = skip_articles | concat: temp %}
<div class="well">
<h3><a href="{{ related_article.url }}">{{ related_article.title }}</a></h3>
{% if related_article.excerpt.size > 0 %}
<p>{{ related_article.excerpt }}</p>
{% else %}
<p>{{ related_article.content | truncatewords: 40 }}</p>
{% endif %}
</div>
{% endunless %}
{% endif %}
{% endfor %}
{% endfor %}
</div>