I want to make a specific picture appear based on what menu item is selected or which collection is being viewed but i cannot figure out what is wrong with the code.
{% if collection.handle == ‘Tops’ %}
{% endif %}
A user is attempting to implement conditional formatting to display different images based on the selected menu item or collection being viewed, but their Liquid code contains multiple errors.
Main Issues Identified:
collection.handle == 'Tops' should use 'tops' instead<img> element is improperly formatted and wrapped inside HTML comment tags, preventing it from renderingRecommended Solution:
A community member provided corrected code that:
downcase filter to ensure proper handle comparison<img> tag outside of commentsAdditional advice included being cautious when using AI tools like ChatGPT for code generation, as they can introduce errors that create more work if the output cannot be validated against existing knowledge.
I want to make a specific picture appear based on what menu item is selected or which collection is being viewed but i cannot figure out what is wrong with the code.
{% if collection.handle == ‘Tops’ %}
{% endif %}
Hi @kashbunny
handles are lower cased https://shopify.dev/docs/api/liquid/basics#handles
and the img element is not properly bracketed and it’s inside html comments.
If you are using chatgpt etc use such tools for summarizing knowledge & experience you can validate otherwise you cannot see the misinformation such tools create and just create more work for everyone.
{%- assign collection_title_lowercased = 'Tops' | downcase -%}
{%- assign image_filename = 'tops_4.png'
{% if collection.handle == collection_title_lowercased %}
{% endif %}
{% comment %}
https://shopify.dev/docs/api/liquid/filters/downcase
https://shopify.dev/docs/api/liquid/filters/file_img_url
{% endcomment %}
the width attribute as percentage will work though technically invalid. https://stackoverflow.com/questions/26100484/what-are-the-consequences-of-using-percentage-for-width-and-height-attributes-of