Brandy
Brandy

Reputation: 554

Shopify check if image exists

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.

Upvotes: 4

Views: 6337

Answers (3)

Debro Roy
Debro Roy

Reputation: 11

I solved this using "images" object:

{% assign custom_image = images['my-image.jpg'] %}

{% if custom_image %} 

{{ custom_image | image_url: width: 300 | image_tag }}

{% endif %}

Ref: https://shopify.dev/api/liquid/objects#images

Upvotes: 1

Mahabubur Rahman
Mahabubur Rahman

Reputation: 160

In case anyone trying to check if an image coming from a block or section settings in liquid. Then you can check like this.

For section

{% if section.settings.image != nil %}
            <div class="image">
                {{ block.settings.image | image_url: width: 400 | 
image_tag }}
            </div>
{% endif %}

For block

{% if block.settings.image != nil %}
            <div class="image">
                {{ block.settings.image | image_url: width: 400 | 
image_tag }}
            </div>
{% endif %}

Upvotes: 0

MyShopify Consultants
MyShopify Consultants

Reputation: 136

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"
  },

Upvotes: 1

Related Questions