简体   繁体   English

如果Shopify主题的search.liquid中的所有search.results都没有item.featured_image,如何显示不同的内容

[英]How to display different content IF all of the search.results do not have item.featured_image in Shopify Theme's search.liquid

I began working with a company that already had quite a complex theme built and live. 我开始与一家已经建立并运行了一个非常复杂的主题的公司合作。 On their collection pages and search page, the products that don't have a featured image don't get displayed. 在收藏页面和搜索页面上,没有显示没有特色图片的产品。 So when searching if all of the results are items that don't have featured images, all there is now is a blank space where products would be. 因此,当搜索所有结果是否都是没有特色图片的商品时,现在所有商品都会留有空白。

I'm wanting to know if it is possible in the search.liquid file to check if all the search.results are missing an item.featured_image , if that's true, then skip showing the results and display a message stating there's no results. 我想知道是否有可能在search.liquid文件中检查所有search.results是否都缺少item.featured_image ,如果是真的,那么跳过显示结果并显示一条消息,指出没有结果。

A stripped down example of the code now is as follows: 现在,该代码的精简示例如下:

{% if search.performed %}
{% paginate search.results by 60 %}

{% if search.results == empty %}
<div class="row">
  <h2>Sorry! No Search Results</h2>
  <p>Your search for <strong>"{{ search.terms }}"</strong> didn't
 return any results.</p>
</div>

{% else %}

<div class="row">
  <h1>Search Results</h1>
  <p>{{ search.results_count }} Products match your search.</p>
  {% include 'pagination' %}
</div>


{% for item in search.results %}
  {% if item.featured_image == blank %}
    {% continue %}
  {% endif %}

  // looped code for each item with featured image.

{% endfor %}

{% endif %}

{% endpaginate %}

{% endif %}

Any help would be greatly appreciated! 任何帮助将不胜感激!

----EDIT---- - - 编辑 - -

As HymnZ led me to find, simply creating a for loop to count the items.featured_image == blank and checking if that count matches search.results_count does the trick. 当HymnZ引导我找到时,只需创建一个for循环来对items.featured_image == blank进行计数,然后检查该计数是否与search.results_count匹配即可。

Replacing {% if search.results == empty %} , which determines if the No Results text will display, with the code below solves the problem. 用下面的代码替换{% if search.results == empty %} ,确定是否显示“无结果”文本,下面的代码解决了该问题。

{% assign items_blank = 0 %}
{% for item in search.results %}
  {% if item.featured_image == blank %}
    {% assign items_blank = items_blank | plus: 1 %}
  {% endif %}
{% endfor %}

{% if search.results == empty or items_blank == search.results_count %}

Replace the line {% if search.results == empty %} with the following code 用以下代码替换{% if search.results == empty %}

{% assign items_blank = 0 %}
{% for item in search.results %}
  {% if item.featured_image == blank %}
  {% assign items_blank = items_blank | plus: 1 %}
  {% endif %}
{% endfor %}

{% if search.results == empty or items_blank == search.results_count %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM