如果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 %}
我尝试使用其他一些变体,例如不使用'|。 strip_html”。我试图将其放在这样的引号内:{%if article.tags ==“'”和section.settings.brand-news-tag和“'”%}。我还尝试过使用“包含”而不是“ ==”。
请有人告诉我如何在if语句中使用变量吗?这让我很头疼。
谢谢杰森。
-------
我尝试使用-
{% if article.tags contains section.settings.brand-news-tag %}
我还尝试了没有任何if语句来缩小blogs.news的范围。这按预期工作。这意味着if语句不能与Blog标签进行比较。尽管我已经直接从博客文章中复制了博客标签,以进入本节中的变量。
这也不起作用-
{% 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 %}
这也不起作用(Goodfellow是复制的标签)-
{% for article in blogs.news.articles limit:1 %}
{% if article.tags contains 'Goodfellow' %}
以下是博客方面的一些图片:
请帮助,杰森。
答案 0 :(得分:1)
如果您查看Shopify documentation中的 article 对象,则 article.tags 返回一个数组。因此,不能使用 == 等于运算符将数组与字符串进行比较。您正在按照logical and comparison operators documentation中的说明查找容器。
因此您的代码将变为
{% if article.tags contains section.settings.brand-news-tag %}
此外,您不需要 strip_html ,因为字段类型是文本。因此Shopify会照顾好它。您可以使用 strip 过滤器从字符串的开头和结尾删除所有空格和制表符,以确保更加安全。
对于您的特定情况,您不需要for循环内的 limit:1 ,因为您不知道第一个对象将包含该标记。因此,您需要遍历所有对象并在满足条件的情况下跳出循环。示例代码
{% for article in blogs.news.articles %}
{% if article.tags contains 'Goodfellow' %}
{{article.tags}}
{% break %}
{% endif %}
{% endfor %}