I'm looking to be able to return the info from a field(s) from a Matrix row where I know which row I want (long story). I had a guess at various ways of doing this, but in the end the only way that worked was to run the loop with an if statement to limit the output to a given row's id, e.g:

{% for block in entry.bodyRepeater %}
    {% if loop.index==2 %}
    <h3>{{ block.heading }}</h3>
    {% endif %}
{% endfor %}

Guessing there might be a better way though?


This works but using this code can be a problem if you are dealing with a huge number of rows.

To access the n-th row, you can use:

{{ entry.blockRepeater.findElementAtOffset(n-1).heading }}

I found this method in the ElementCriteriaModel class. You can test if a certain row exists using entry.blockRepeater.offsetExists(n).

{% set block = entry.bodyRepeater.nth(2) %}

{% if block %}
    <h3>{{ block.heading }}</h3>
{% endif %}

Craft 2.2 introduced the nth method to ElementCriteriaModel objects for fetching the element at a specific offset. It's more convienient to use this instead of a combination of the undocumented offsetExists and findElementAtOffset methods.


I managed to do this for a quiz, getting the row from the url

{% set theRow = craft.request.getParam('row') %} 

Just remember zero is the first row.

  • entry.matrixField[1] queries for all the blocks, so it isn't a whole lot more efficient then the loop approach in the question.
    – carlcs
    Apr 19 '16 at 8:41

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.