1

I have an Entry field (page) as part of my Matrix field (footer).

When the client selects 5 entries, I want to display them all in my list and not just the first. I can only seem to get my code working with .first or .last. Everything else I try gives me the old "Object of class Craft\ElementCriteriaModel could not be converted to string" error.

<ul class="footlist">
    <li class="foottitle">{{ block.navSection }}</li>
        {% if block.type == "footerCategory" %}
        {% set page = block.page.first() %}
           <li>{{ page.title }}</li>
        {% endif %} 
</ul>

My matrix for this list: enter image description here

3
  • What happens if you remove the first()?
    – Damon
    Oct 20, 2017 at 3:53
  • I get the error when i remove first
    – kingkelly
    Oct 20, 2017 at 16:35
  • I'm having a similar problem and any help would be greatful. I have this code: {% for entry in craft.entries.section('positions') %} <li class="list-item-career tooltip"> <div class="list-item__title" style="padding: 15px 0px;"> <div class="col-6">{{ entry.title }}</div> <div class="col-6">{{ entry.locationLink.first }}</div> </div> And this pulls my first entry just fine,. but i now need to list them all but when I change .first to .find() I get error. Can you please let me know what I might be doing wrong?
    – Joe
    Jan 18, 2023 at 15:59

2 Answers 2

1

I think you'll need to remove the first() part. Then your twig will look something like this...

<ul class="footlist">
    <li class="foottitle">{{ block.navSection }}</li>
        {% if block.type == "footerCategory" %}
            {% set pages = block.page %}
            {% if pages|length %}
                {% for page in pages %}
                    <li>{{ page.title }}</li>
                {% endfor %}
            {% end if %}
        {% endif %}
    </li> 
</ul>
1
  • Holy smokes that worked! Thanks! Didnt think i would need that extra if and for, but it works!
    – kingkelly
    Oct 20, 2017 at 16:34
1

The elementCriteriaModel is a class/object that handles your search. It's a class that combines multiple methods to fetch entries from your database with an iterable interface on it.

That means: when you receive an entry you get all the queries you need attached to it to pull all the relationship instead of loading all the other entries all the time. So entry.matrix is nothing but database command to pull the other entries which makes craft fast. Otherwise you would take much more time.

To make it short: when you loop through your elementCriteriaModel the method .find() is executed, that fetches all your entries and return them as an array. The method first() returns only the first element, last() returns only the last element.

In order to pull all you just need to loop block.page {% for entry in block.page %} will to it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.