Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T21:29:12.427Z
Answers.Text: Ah no, relatedImages are entries with one image in each of those entries.
Answers.UserDisplayName: John
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T21:52:18.930Z
Answers.Text: 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?
Answers.UserDisplayName: John
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T21:59:02.763Z
Answers.Text: Yup... updated answer.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T22:01:14.547Z
Answers.Text: Aha! For some reason my googling let me down there. Cheers, your Bradness!
Answers.UserDisplayName: John
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T21:22:06.380Z
Answers.LastActivityDate: 2014-12-06T21:57:52.040Z
Answers.LastEditDate: 2014-12-06T21:57:52.040Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T20:05:30.800Z
Answers.Text: I tried exactly that and it doesn't work for me.
Answers.UserDisplayName: John
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T20:19:19.030Z
Answers.Text: 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.
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T20:32:56.867Z
Answers.Text: Let me show the full code and you can tell me better if that is the case? I'll edit the question.
Answers.UserDisplayName: John
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T20:35:18.083Z
Answers.Text: There, updated, it's an asset field with multiple images in it.
Answers.UserDisplayName: John
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T20:49:24.207Z
Answers.Text: 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 %}
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T20:50:05.530Z
Answers.Text: I tried that, it didn't like it!
Answers.UserDisplayName: John
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T20:53:44.357Z
Answers.Text: Does this work to loop over the images?
  
      {% for relatedImage in entry.relatedImages %}
        {{loop.index}}: {{relatedImage.title}}
      {% endfor %}
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T21:11:39.180Z
Answers.Text: 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.
Answers.UserDisplayName: John
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T19:44:17.927Z
Answers.LastActivityDate: 2014-12-06T19:44:17.927Z
Answers.UserDisplayName: Marion Newlevant
Body: <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>
  
Tags: <templating><forloop>
Title: Target second (or nth) element in a for loop
ThreadText: # 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 %}
          <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?
  
  **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 %}
          <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 %}
  
  ```
  
  > **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](http://twig.sensiolabs.org/doc/tags/for.html#the-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`.
  
  > **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.  
  
  ---
  
  