skip to Main Content

sadly I’m not able to get this simple piece of code running:

{% for field in product.metafields.custom.anleitung %}
{{ field | first }}: {{ field | last }}
{% endfor %}

I have three files in a file – list metafield and simply want them to get displayed on my site. (number of files varies)

2

Answers


  1. {% for field in product.metafields.custom.anleitung.value %}
      <div><a href="{{ field.url }}">Anleitung downloaden</a></div>
    {% endfor %}
    

    See Accessing metafields of type list for the syntax to loop over list values.

    Instead of field.url, you can use metafield_text filter: <a href="{{ field | metafield_text }}">Anleitung downloaden</a>.

    Or, you can use metafield_tag and rely on Shopify to generate the link: {{ field | metafield_tag }}

    Login or Signup to reply.
  2. For anyone else having trouble getting this to work with multiple pdfs/files, I have a working solution:

    <p>Datasheet technical information for this product.</p>
    
    {% assign file = product.metafields.custom.technical_datasheet.value %}
    
    {% for field in file %}
    {% assign file_output = file %}
     {% assign file_url = field.url %}
      {% assign file_name_with_shopify_extension = file_url | split: '/' | last %} <!-- parse the file name from the URL -->
      {% assign file_name = file_name_with_shopify_extension | split: '?' | first %} <!-- strip off the ?v=123456789 part of the file name -->
      <div><strong>Download:</strong> <a href="{{ field.url }}" target="new">{{ file_name }}</a></div>
    {% endfor %}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search