<p>It's not working because you're using <code>loop.index</code> in the context of the inner loop.</p>

<p>Based on your comment, there will only ever be one images selected, so <code>loop.index</code> will never be 2.</p>

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

<pre><code>{% set counter = 0 %}

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

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


{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-12-06T21:29:12.427Z
Ah no, relatedImages are entries with one image in each of those entries.
John
CC BY-SA 3.0
2014-12-06T21:52:18.930Z
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
CC BY-SA 3.0
2014-12-06T21:59:02.763Z
Yup... updated answer.
Brad Bell
CC BY-SA 3.0
2014-12-06T22:01:14.547Z
Aha! For some reason my googling let me down there. Cheers, your Bradness!
John
CC BY-SA 3.0
2014-12-06T21:22:06.380Z
2014-12-06T21:57:52.040Z
2014-12-06T21:57:52.040Z
Brad Bell
<p>What you need is the twig <a href="http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable" rel="nofollow">loop variable</a>:</p>

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

<p>There are a number of useful loop variables: <code>first</code>, <code>last</code>, <code>index</code>, <code>revindex</code>, <code>length</code>.</p>

CC BY-SA 3.0
2014-12-06T20:05:30.800Z
I tried exactly that and it doesn't work for me.
John
CC BY-SA 3.0
2014-12-06T20:19:19.030Z
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
CC BY-SA 3.0
2014-12-06T20:32:56.867Z
Let me show the full code and you can tell me better if that is the case? I'll edit the question.
John
CC BY-SA 3.0
2014-12-06T20:35:18.083Z
There, updated, it's an asset field with multiple images in it.
John
CC BY-SA 3.0
2014-12-06T20:49:24.207Z
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
CC BY-SA 3.0
2014-12-06T20:50:05.530Z
I tried that, it didn't like it!
John
CC BY-SA 3.0
2014-12-06T20:53:44.357Z
Does this work to loop over the images?

    {% for relatedImage in entry.relatedImages %}
      {{loop.index}}: {{relatedImage.title}}
    {% endfor %}
Marion Newlevant
CC BY-SA 3.0
2014-12-06T21:11:39.180Z
Again no, i don't see how your code can work tbh, it needs to loop over the images in the field, your code is trying to access the field once. From what i can see anyway.
John
CC BY-SA 3.0
2014-12-06T19:44:17.927Z
2014-12-06T19:44:17.927Z
Marion Newlevant
<p>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.</p>

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

<p>Any ideas?</p>

<templating><forloop>
Target second (or nth) element in a for loop
# Target second (or nth) element in a for loop

(Asked by: username_0 on 2014-12-06)

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 %}
        
            
                
             {% if loop.index == 2 %}Welcome!{% endif %}
         
    {% endfor %}
{% endfor %}

Any ideas?

**Tags:** templating, forloop

---

## 2 answers

### Answer by username_1 on 2014-12-06. Score: 3 | Accepted answer

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 %}
        
            
                
             {% if counter == 2 %}Welcome!{% endif %}
         
    {% endfor %}


{% endfor %}

> **Comments:**
> **username_0**: Ah no, relatedImages are entries with one image in each of those entries.  
> **username_0**: 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?  
> **username_1**: Yup... updated answer.  
> **username_0**: Aha! For some reason my googling let me down there. Cheers, your Bradness!  

---

### Answer by username_2 on 2014-12-06. Score: 1

What you need is the twig loop variable:
{% for relatedImage in entry.relatedImages %}
  {% for images in relatedImage.image %}
    
  {% endfor %}
{% endfor %}

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

> **Comments:**
> **username_0**: I tried exactly that and it doesn't work for me.  
> **username_2**: 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.  
> **username_0**: Let me show the full code and you can tell me better if that is the case? I'll edit the question.  
> **username_0**: There, updated, it's an asset field with multiple images in it.  
> **username_2**: 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 %}  
> **username_0**: I tried that, it didn't like it!  
> **username_2**: Does this work to loop over the images?      {% for relatedImage in entry.relatedImages %}       {{loop.index}}: {{relatedImage.title}}     {% endfor %}  
> **username_0**: Again no, i don't see how your code can work tbh, it needs to loop over the images in the field, your code is trying to access the field once. From what i can see anyway.  

---

