如何显示不同的内容如果所有的search.results在Shopify Theme的search.liquid中没有item.featured_image

时间:2016-02-09 05:22:23

标签: shopify liquid

我开始与一家已经拥有相当复杂的主题的公司合作并建立起来。在其收藏页面和搜索页面上,不显示没有精选图像的产品。因此,当搜索所有结果都是没有特色图像的项目时,现在所有产品都是空白区域。

我想知道<script> function runOnecode() { var str = ""; str += $(".google-visualization-controls-rangefilter-thumblabel").text(); var until = str.substring(10); var from = str.substring(0,10); var center = $("#centerSelect option:selected").text(); var query = [ ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND G =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND H =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND I =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND J =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND K =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND N =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND O =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND P =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND Q =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND R =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND S =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND T =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND U =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND V =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND W =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND X =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND Y =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND Z =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND AA =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND AB =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND AC =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND AD =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND AE =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND AF =\"No\" "), ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A) >=date \'" + from + "\' AND todate(A) <=date \'" + until + "\' AND AG =\"No\" ") ]; for(var i=0; i<25; i++){ getData(query[i]); // When all are done I will draw the table. } } </script> <script> function getData(incomingQuery) { // Prepare the query var queryString = encodeURIComponent(incomingQuery); var query = new google.visualization.Query( //Collect the data from this spreadsheet 'MySpreadSheet URL' + queryString); query.send(handleSampleDataQueryResponse); //Handle any errors from the Google server function handleSampleDataQueryResponse(response) { if (response.isError()) { alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage()); return; } // get look into the resulting data and find the detail. var datas = response.getDataTable(); console.log(datas); if (datas.Gf.length === 0) { resultData = 0; } else resultData = datas.Gf[0].c[0].v; allData.push(resultData); console.log(allData); } } </script> 文件中是否有可能检查所有search.liquid是否缺少search.results,如果是,则跳过显示结果和显示一条消息,说明没有结果。

现在代码的精简示例如下:

item.featured_image

非常感谢任何帮助!

---- ---- EDIT

正如HymnZ让我发现的那样,只需创建一个for循环来计算{% 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 %} 并检查该计数是否与items.featured_image == blank相匹配就可以了。

更换search.results_count,确定是否显示无结果文本,并使用以下代码解决问题。

{% if search.results == empty %}

1 个答案:

答案 0 :(得分:1)

使用以下代码替换行{% 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 %}