Assign a specific GID to a variable in "if/else"

Hi,

I have an if/else statement that checks if a file metafield has a value in it, and if it’s empty, it should use a specific GID that I’m putting directly in the liquid code block.

The desired purpose is to display a backup image when the object doesn’t have an image selected.

The problem I have is that an identical GID doesn’t work when assigned as a string, while it does work when assigned from the metafield. How do I need to format this GID string so that it will work when assigned “manually”?

{% for item in shop.metaobjects.metaobject_name.values %}
    {% if item.brand.value.brand != blank %}
        
        {% if item.top_image != blank %}
            {% assign card_image = item.top_image %}
        {% else %}
            **{% assign card_image = 'gid://shopify/MediaImage/62419296813284' %}**
        {% endif %}
        
        <a href="{{item.metaobject_system.url}}" style="text-decoration: none;">
        <div class="repeater-card">
            <div class="repeater-image-container">
                <img src="**{{ card_image | image_url }}**" alt="{{item.title}}" loading="lazy"> 
            </div>
            <h4>{{ item.title }}</h4>
            <p>{{ item.intro_title }}</p>
        </div>
        </a>
    {% endif %}
{% endfor %}

You could try some additional troubleshooting by pasting the following code just below the if/else block where card_image is assigned. This will log the output to the console, allowing you to see if there are any differences. Feel free to share a screenshot of the output for both cases — with and without the metafield — and we can continue troubleshooting from there.


Solution: Use the Shopify URL instead of the GID.

  • Also must move the image_url filter inside the variable definition. Example: {%- assign card_image = item.top_image | image_url -%}
  • Also remove the image_url filter from tags where you are using the variable. Example:

Example:

{%- if item.top_image != blank -%}
{%- assign card_image = item.top_image | image_url -%}
{%- else -%}
{%- assign card_image = ‘https://123abc.myshopify.com/cdn/shop/files/file-name.png’ -%}
{%- endif -%}

{{item.title}}

{{ item.title }}

{{ item.intro_title }}