2

Morning Craft folks, I've had a look and play but can't seem to find an answer for this.

I have a global variable with a company's various phone numbers in, stored in a table field.

The table shows the city, then the phone number for the company's numbers.

For the navigation, I want to show just the first phone number in the table.

.first() and .limit(1) don't seem to work in this situation.

<li>
    {% if companyDetails.telephoneNumbers | length %}
        {% for row in companyDetails.telephoneNumbers %}
            <a href="#contact-us" role="menuitem" aria-selected="false">{{ row.number }}</a>
        {% endfor %}
    {% endif %}

    <span>Call us</a></span>
</li>

Many thanks in advance.

2

first() and limit() are methods of the ElementCriteriaModel. Since a table is not an element, but just a plain array, you should use array filters.

So use Twig's slice filter instead.

<li>
    {% if companyDetails.telephoneNumbers | length %}
        {% for row in companyDetails.telephoneNumbers | slice(0, 1) %}
        <a href="#contact-us" role="menuitem" aria-selected="false">{{ row.number }}</a>
        {% endfor %}
    {% endif %}
    <span>Call us</a></span>
</li>
1
  • Thanks Bob, this worked great. I saw the slice else where but misunderstood its purpose.
    – Pathfinder
    Jun 23 '15 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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