1

I'm trying to show content for only the second entry in a for loop, but can't find any info on how to do specifically this. I'd like to do something like the below.

{% for relatedImage in entry.relatedImages %}
    {% for images in relatedImage.image %}
        <div>
            <a href="{{ relatedImage.url }}">
                <img src="{{ images.getUrl('default') }}" alt="{{ relatedImage.title }}" width="{{ images.getWidth('default') }}" height="{{ images.getHeight('default') }}" />
             </a>{% if loop.index == 2 %}<div><h2>Welcome!</h2></div>{% endif %}
         </div>
    {% endfor %}
{% endfor %}

Any ideas?

3

It's not working because you're using loop.index in the context of the inner loop.

Based on your comment, there will only ever be one images selected, so loop.index will never be 2.

If you don't want to move the loop.index check to the outer loop, then a simple counter will work:

{% set counter = 0 %}

{% for relatedImage in entry.relatedImages %}
    {% set counter = counter + 1 %}

    {% for images in relatedImage.image %}
        <div>
            <a href="{{ relatedImage.url }}">
                <img src="{{ images.getUrl('default') }}" alt="{{ relatedImage.title }}" width="{{ images.getWidth('default') }}" height="{{ images.getHeight('default') }}" />
             </a>{% if counter == 2 %}<div><h2>Welcome!</h2></div>{% endif %}
         </div>
    {% endfor %}


{% endfor %}
  • Ah no, relatedImages are entries with one image in each of those entries. – John Dec 6 '14 at 21:29
  • Well, not quite as i need the content of that {if} inside the div inside the loop. Is it possible to implement a counter in the outer loop (like in php $1++) that i can inspect in the inner loop? – John Dec 6 '14 at 21:52
  • Yup... updated answer. – Brad Bell Dec 6 '14 at 21:59
  • Aha! For some reason my googling let me down there. Cheers, your Bradness! – John Dec 6 '14 at 22:01
1

What you need is the twig loop variable:

{% for relatedImage in entry.relatedImages %}
  {% for images in relatedImage.image %}
    <blah {% if loop.index == 2%}class="i'm the second"{% endif %} >
  {% endfor %}
{% endfor %}

There are a number of useful loop variables: first, last, index, revindex, length.

  • I tried exactly that and it doesn't work for me. – John Dec 6 '14 at 20:05
  • I am a little suspicious of your nested for loops. If the relatedImages field is just a regular asset, then you only need the outer loop. – Marion Newlevant Dec 6 '14 at 20:19
  • Let me show the full code and you can tell me better if that is the case? I'll edit the question. – John Dec 6 '14 at 20:32
  • There, updated, it's an asset field with multiple images in it. – John Dec 6 '14 at 20:35
  • I think you just need the single loop: {% for relatedImage in entry.relatedImages %} <div> <a href="{{ relatedImage.url }}"> <img src="{{ relatedImage.getUrl('default') }}" alt="{{ relatedImage.title }}" width="{{ relatedImage.getWidth('default') }}" height="{{ relatedImage.getHeight('default') }}" /> </a> {% if loop.index == 2 %}<div><h2>Welcome!</h2></div>{% endif %} </div> {% endfor %} – Marion Newlevant Dec 6 '14 at 20:49

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.