How to display different metafield texts for each image?

Solved

How to display different metafield texts for each image?

PhilippOcculto
Excursionist
30 1 2

Unbenannt.PNG

 Hey, 

 

at the moment i show the text beneath the images with metafields, but under every image the text is the same. 

I want to show for every picture a different text with metafields, but in the code the images load with for.

 

Is this possible with the alt-text.?for example: if the alt-text contain "2" , show the metafield image_description_1

 

Best regards 

Philipp

Accepted Solution (1)

NomtechSolution
Astronaut
1245 113 159

This is an accepted solution.

To display different text for each image using metafields and alt-text, you can use conditional statements in your code. Here's an example of how you can achieve this:

Assuming you have a loop that iterates through the images, you can check the alt-text of each image and display the corresponding metafield value. Here's a simplified code snippet:

{% for image in product.images %}
  <img src="{{ image.src }}" alt="{{ image.alt }}">
  {% assign imageNumber = image.alt | remove: 'Image ' | plus: 0 %}

  {% if imageNumber == 1 %}
    <p>{{ product.metafields.namespace.image_description_1 }}</p>
  {% elsif imageNumber == 2 %}
    <p>{{ product.metafields.namespace.image_description_2 }}</p>
  {% elsif imageNumber == 3 %}
    <p>{{ product.metafields.namespace.image_description_3 }}</p>
  {% endif %}
{% endfor %}

View solution in original post

Reply 1 (1)

NomtechSolution
Astronaut
1245 113 159

This is an accepted solution.

To display different text for each image using metafields and alt-text, you can use conditional statements in your code. Here's an example of how you can achieve this:

Assuming you have a loop that iterates through the images, you can check the alt-text of each image and display the corresponding metafield value. Here's a simplified code snippet:

{% for image in product.images %}
  <img src="{{ image.src }}" alt="{{ image.alt }}">
  {% assign imageNumber = image.alt | remove: 'Image ' | plus: 0 %}

  {% if imageNumber == 1 %}
    <p>{{ product.metafields.namespace.image_description_1 }}</p>
  {% elsif imageNumber == 2 %}
    <p>{{ product.metafields.namespace.image_description_2 }}</p>
  {% elsif imageNumber == 3 %}
    <p>{{ product.metafields.namespace.image_description_3 }}</p>
  {% endif %}
{% endfor %}