I created one field for an image and In the field setting, I made allowed number of values to 2 (please find the attached screenshot

enter image description here)

So my question is, how can I add both the images in a different div tag in my page template.

Or, how can I target first and second image in my page template.

  • Welcome to Drupal Answers! Can you please update your question and add the code you are currently using to print this field inside what template exactly? Many thanks – leymannx Jun 22 '19 at 19:09
  • Possible duplicate of Render a node field inside page.html.twig – leymannx Jun 22 '19 at 19:26

There are many ways to achieve this. Given you use a custom or sub-theme you'd follow Twig Template naming conventions and create your own field--node--MYFIELDNAME.html.twig template. And then simply adjust the markup according your needs.

See Classy's field.html.twig for reference and its for-loop you simply can customize:

{% for item in items %}
  <div{{ attributes.addClass(classes, 'field__item') }}>
    {{ item.content }}
{% endfor %}

To target the second item in a multi-value field inside page.html.twig use the following snippet (it starts to count with 0, so that 0 is the first and 1 is the second item).

{{ node.field_MYFIELD.1.value }}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.