Ad

Shopify Check If Image Exists

- 1 answer

I added an image upload setting to my Theme's settings. It's id is slider.jpg and I want to know what the liquid function is to check if the image exists. I don't want to show the image if it doesn't exist yet ( Hasn't been uploaded in the theme settings, in admin.

Ad

Answer

Just do

{% for i in (1..6) %}
  {% assign display_slide = 'display_slide_' | append: forloop.index %}
  {% assign slide_img = 'slider_' | append: forloop.index | append: '.jpg' %}
  {% if settings[display_slide] %}
    <li>{{ slide_img | asset_url }}</li>
  {% endif %}
{% endfor %}

and then in your settings file do this for numbers 1 - 6

  {
    "type": "checkbox",
    "id": "display_slide_1",
    "label": "Display Slide One?"
  },{
    "type": "image",
    "id": "slider_1.jpg",
    "label": "Slide Image One"
  },
  {
    "type": "checkbox",
    "id": "display_slide_2",
    "label": "Display Slide Two?"
  },{
    "type": "image",
    "id": "slider_2.jpg",
    "label": "Slide Image Two"
  },
Ad
source: stackoverflow.com
Ad