Show video if product.media contains video else image

I want to show a video (not within gallery), if the product has a video. If not, the second image should display.

{% case media.media_type %} does not help. {% if media.alt contains ‘x’ %} would help, but would mean too much work.

I tried this code, it shows the video, but not the image.

{% assign videos = product.media | where: 'media_type', 'video' %}
{% if videos %}
  {% for media in product.media %}
    {% if media.media_type == 'video' %}
      video
    {% endif %}
  {% endfor %}
{% else %}
  {% for media in product.media offset:1 limit:1 %}  
    {% if media.media_type == 'image' %}
      second image
    {% endif %}
  {% endfor %}
{% endif %}
{% assign videos = product.media | where: 'media_type', 'video' %}
{% if videos.size > 0 %}
  {% for media in product.media %}
    {% if media.media_type == 'video' %}
      video
    {% endif %}
  {% endfor %}
{% else %}
  {% for media in product.media offset:1 limit:1 %}  
    {% if media.media_type == 'image' %}
      second image
    {% endif %}
  {% endfor %}
{% endif %}

You can try this.

1 Like

Works. Thanks so much.