我正在尝试浏览相关的博客文章,如果article.tags,==,字符串给出的部分设置。
我试过几种不同的变体,但没有效果。我的代码如下所示(不要担心if循环中的内容,这都是由CSS固定的):
{% for article in blogs.news.articles limit:1 %}
{% if article.tags == section.settings.brand-news-tag | strip_html %}
{% assign image_src = article.image.src | img_url: 'large' %}
<div class="brand-page-featured-news-blogs">
<div class="brand-page-featured-news-article">
<a href="{{ article.url }}" class="box-link"></a>
<div class="brand-page-featured-news-article-image" style="background-image: url({{ image_src }})">
<div class="brand-page-featured-news-article-image-contain">
<div class="brand-page-featured-news-article-image-overlay">
</div>
</div>
</div>
<div class="brand-page-featured-news-article-contain">
<h6 class="brand-page-featured-news-article-title">{{ article.title }}</h6>
<div class="brand-page-featured-news-article-content">
<p class="brand-page-featured-news-article-published">{{ article.published_at | date: "%d %B 20%y" }}</p>
<p class="brand-page-featured-news-article-text">{{ article.content }}</p>
<div class="brand-page-featured-news-article-button">
<div class="brand-page-featured-news-article-button-text">
Read More
</div>
</div>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
{% schema %}
{
"name": "Featured News",
"settings": [
{
"id": "brand-news-tag",
"type": "text",
"label": "Brand News Tag",
"default": "brandnametag"
}
]
}
{% endschema %}
{% stylesheet %}
{% endstylesheet %}
{% javascript %}
{% endjavascript %}这就是问题所在:
{% if article.tags == section.settings.brand-news-tag | strip_html %}我尝试使用一些其他的变体,比如没有‘扣子_html’。我试着把它放进这样的引号里,{% if article.tags ==“‘和section.setings.brand-news-tag和”’‘%}。我也试着用'contains‘而不是an '==’。
如何在if语句中使用变量?
我试过用-
{% if article.tags contains section.settings.brand-news-tag %}我也尝试过没有任何if语句缩小blogs.news。这如预期的那样起作用。这意味着这与if语句有关,而不是与博客标记相比较。虽然我已经从博客文章中直接复制了博客标签,但在这一节中进入了变量。
这也不管用-
{% for article in blogs.news.articles limit:1 %}
{% if section.settings.brand-news-tag != '' %}
{% assign blogfilter = section.settings.brand-news-tag | strip %}
{% endif %}
{% if article.tags contains blogfilter %}这也不管用(古德费罗是复制的标签)-
{% for article in blogs.news.articles limit:1 %}
{% if article.tags contains 'Goodfellow' %}以下是博客方面的一些图片:



发布于 2019-08-22 21:10:09
如果您查看文章对象在Shopify文档中,那么article.tags返回一个数组。因此,不能将数组与使用==等于运算符的字符串进行比较。正如在逻辑和比较操作符文档中所解释的那样,您正在寻找包含。
所以你的代码会变成
{% if article.tags contains section.settings.brand-news-tag %}此外,您不需要strip_html,因为字段类型是文本。所以Shopify会处理好的。您可以使用条形筛选器从字符串的开头和结尾删除任何空格和制表符,这是非常肯定的。
对于您的特定场景,您不需要限制: 1在For循环中,因为您不知道第一个对象将包含该标记。因此,如果满足条件,则需要对所有对象进行迭代,并中断循环。样本代码
{% for article in blogs.news.articles %}
{% if article.tags contains 'Goodfellow' %}
{{article.tags}}
{% break %}
{% endif %}
{% endfor %}https://stackoverflow.com/questions/57613567
复制相似问题