Re: Accessing metaobject field with list of files type

Accessing metaobject field with list of files type

geordiedigital
Shopify Partner
2 0 1

I have a metafield in my page which accepts a metaobject as its input type. In a section referencing this page metaobject I can access the fields which have a single input type no problem using this:

 

{% assign myMetaobject = page.metafields.custom.example.value %}

 

This lets me access fields with single or multi line text inputs using myMetaobject.title or myMetaobject.description. But I also have a field which accepts multiple files, specifically image and video. This is where I am struggling to access the files in a loop. 

 

If I try this {{ myMetaobject.images }} it prints:

 

 ["gid://shopify/MediaImage/25857330184378","gid://shopify/MediaImage/25863258636474","gid://shopify/MediaImage/25863258570938"]

 

If I try to do a for loop on myMetaobject.images, it doesnt print anything out. Any ideas?

Replies 4 (4)

iffikhan30
Shopify Partner
275 37 51
{% for image in myMetaobject.images %}
        <div class="item-cell">
          <img class="lazyload"
            src=" {{ image | image_url: width: '1640x' }}"
            data-widths="[475, 880, 1200, 1620]"
            data-aspectratio="{{ image.aspect_ratio }}"
            data-sizes="auto" />
          <noscript>
            <img class="grid-product__image lazyloaded"
              src="{{ field | image_url: width: '1640x' }}">
          </noscript>
        </div>
      {% endfor %}

 

Here is beautify code for you, kindly accept my answer.

 

Thanks

Custom theme and app [remix] expert.

Email: irfan.sarwar.khan30@gmail.com
Chat on WhatsApp
AnonymousSherlo
Visitor
1 0 0

Hii bro do you know how we can access single image from a file list metafield using index without for loop?

iffikhan30
Shopify Partner
275 37 51

{% assign myMetaobject = page.metafields.custom.example.value %}

use this for single

Custom theme and app [remix] expert.

Email: irfan.sarwar.khan30@gmail.com
Chat on WhatsApp

jomedina
Shopify Partner
3 0 0

You can access the fields inside a metaobject in this way:

{% for metaobject in product.metafields.custom.metafield_key_name.value %}
  {% if metaobject.system.type == "metaobject_identifier" %}
    {{ metaobject.title }}
    {{ metaobject.content | metafield_tag }}
  {% endif %}
{% endfor %}