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 block
is 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?
.first()
before.block
? I think it has something to do with the order of this line.block
..find()
at the end?