I am trying to find a way to ask the question with some kind of conditional statement, if the search results on the search results page contain specific types (e.g. product, article). If I can get that information, then I can style the search results page accordingly, based on whether articles + products appear, or just articles, or just products. The layout needs to be quite different depending on what the results are, which types appear, and that’s my goal with the question here.
Below are the code examples from my files, that contain the most important information.
main-search.liquid file:
{%- render 'predictive-search', context: 'search-page' -%}
{%- if search.performed -%}
{%- if search.results_count == 0 -%}
{{ 'general.search.no_results_html' | t: terms: search.terms | replace: '*', '' }}
{%- endif -%}
{%- if search.results_count != 0 -%}
{%- assign paginate_by = per_row | times: page_per_row -%}
{%- paginate search.results by paginate_by -%}
{% render 'search-collection-grid',
collection: search,
items: search.results,
enable_sidebar: section.settings.enable_sidebar,
filter_style: section.settings.filter_style,
enable_sort: false,
enable_collection_count: true
%}
{%- if paginate.pages > 1 -%}
{%- render 'pagination', paginate: paginate -%}
{%- endif -%}
{%- endpaginate -%}
{% endif %}
{%- endif -%}
search-collection-grid.liquid file:
{% liquid
if collection.products
assign count = collection.products_count
assign count_label = 'collections.general.items_with_count'
endif
if collection.results
assign count = collection.results_count
assign count_label = 'general.search.result_count'
endif
%}
{%- for item in items -%}
{%- if item.object_type == 'product' -%}
{% if forloop.index == 1 %}
### Products
{% endif %}
{%- render 'search-product-grid-item', product: item -%}
{%- endif -%}
{%- endfor -%}
### Recipes
{%- for item in items -%}
{%- if item.object_type != 'product' -%}
{%- render 'search-recipe-grid-item', item: item %}
{%- endif -%}
{%- endfor -%}
search-recipe-grid-item.liquid file:
{%- if item.object_type == 'article' and item.image -%}
{%-
render 'image-element',
img: item.image,
classes: 'grid-product__image',
alt: item.title,
widths: '180, 360, 540',
sizeVariable: sizeVariable,
fallback: fallback,
-%}
{%- endif -%}
{{ item.title }}
I tried to wrap all kinds of different conditionals around for {% item in items %} forloop, to check if any products came through the search, but nothing in the Shopify docs or any of the search object or predictive_search object conditionals and options worked to ask that question. It seems like it should be really simple to ask, on the search results page, what object type has come through the search? Then to apply styles etc accordingly. Why should product or article headings or styling appear when that object type isn’t there? Makes no sense. Anyway, happy to receive any guidance on this.