<p>I could be not following correctly, so I apologize if I'm way off here.</p>

<p>If you want to grab <em>all</em> entries that are selected in your <code>block</code> field, but only want to single out the first one, you could do something like this:</p>

<pre><code>&lt;h2&gt;Program Points&lt;/h2&gt;
{% for entry in craft.entries.section('program_point') %}
    {% if loop.first %}
        &lt;h1&gt;{{ entry.title }}&lt;/h1&gt;
    {% else %}
        &lt;h3&gt;{{ entry.title }}&lt;/h3&gt;
    {% endif %}
{% endfor %}
</code></pre>

<p>The <a href="http://twig.sensiolabs.org/doc/tags/for.html" rel="nofollow">twig for loop</a> has some pretty cool stuff built in that you could leverage to check for the first entry.</p>

<p>Hope this helps!</p>

CC BY-SA 3.0
2016-04-06T15:17:57.497Z
Thanks for the answer!
But I think this is not what I'm searching for. I only want the entries where the first element in `block` has the title 'first-block'. Maybe now you understand what I mean?
Jonathan Schiffner
CC BY-SA 3.0
2016-04-06T15:27:14.783Z
Will you be having more than one entry with a title of `first-block`?
Damon
CC BY-SA 3.0
2016-04-06T16:20:17.967Z
yes i will have
Jonathan Schiffner
CC BY-SA 3.0
2016-04-06T15:04:16.723Z
2016-04-06T15:04:16.723Z
Damon
<p>I want to get the title of the first element from a entries field in a for loop. I think its easier to understand with a code example: </p>

<pre><code>{% set programs = craft.entries.section('program_point').block.first().title('first-block') %}
{% for program in programs %}
...
{% endfor %}
</code></pre>

<p><code>block</code> is an entries field. And the loop should only consider entries where the title of the first element in <code>block</code>is <em>first-block</em>.</p>

<p>When I tried this, I always got an error: </p>

<blockquote>
  <p>Impossible to invoke a method ("title") on a null variable</p>
</blockquote>

<p>But when I did this:</p>

<pre><code>{% set programs = craft.entries.section('program_point') %}
{% for program in programs %}
        {% if program.block.first().title == 'first-block' %}
             ...
        {% endif %}
{% endfor %}
</code></pre>

<p>all worked fine. 
So how can I check the title of the first element in the for loop?</p>

CC BY-SA 3.0
2016-04-06T12:29:55.880Z
Have you tried to put `.first()` before `.block` ? I think it has something to do with the order of this line.
KSPR
CC BY-SA 3.0
2016-04-06T12:43:16.707Z
I think this returns the first entry of the section *program_point*. But I need the first element of `block`.
Jonathan Schiffner
CC BY-SA 3.0
2016-04-06T14:59:18.567Z
Yeah you're right. What about adding `.find()` at the end?
KSPR
CC BY-SA 3.0
2016-04-06T15:14:13.730Z
Sadly the same result
Jonathan Schiffner
<templating><forloop><entries-field>
for loop: get the title of the first element of an entries field
# for loop: get the title of the first element of an entries field

(Asked by: username_0 on 2016-04-06)

I want to get the title of the first element from a entries field in a for loop. I think its easier to understand with a code example: 
{% set programs = craft.entries.section('program_point').block.first().title('first-block') %}
{% for program in programs %}
...
{% endfor %}

block is an entries field. And the loop should only consider entries where the title of the first element in blockis first-block.
When I tried this, I always got an error: 

Impossible to invoke a method ("title") on a null variable

But when I did this:
{% set programs = craft.entries.section('program_point') %}
{% for program in programs %}
        {% if program.block.first().title == 'first-block' %}
             ...
        {% endif %}
{% endfor %}

all worked fine. 
So how can I check the title of the first element in the for loop?

**Tags:** templating, forloop, entries-field
> **Comments:**
> **username_1**: Have you tried to put `.first()` before `.block` ? I think it has something to do with the order of this line.  
> **username_0**: I think this returns the first entry of the section *program_point*. But I need the first element of `block`.  
> **username_1**: Yeah you're right. What about adding `.find()` at the end?  
> **username_0**: Sadly the same result  

---

## 1 answers

### Answer by username_2 on 2016-04-06. Score: 1

I could be not following correctly, so I apologize if I'm way off here.
If you want to grab all entries that are selected in your block field, but only want to single out the first one, you could do something like this:
Program Points
{% for entry in craft.entries.section('program_point') %}
    {% if loop.first %}
        {{ entry.title }}
    {% else %}
        {{ entry.title }}
    {% endif %}
{% endfor %}

The twig for loop has some pretty cool stuff built in that you could leverage to check for the first entry.
Hope this helps!

> **Comments:**
> **username_0**: Thanks for the answer! But I think this is not what I'm searching for. I only want the entries where the first element in `block` has the title 'first-block'. Maybe now you understand what I mean?  
> **username_2**: Will you be having more than one entry with a title of `first-block`?  
> **username_0**: yes i will have  

---

