<p>Inside of a for-loop you have access to some <a href="https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable" rel="nofollow noreferrer">special variables</a>.</p>

<pre><code>{% for block in entry.myMatrixField %}
    {% set isEndBlock = loop.revindex &lt;= 3 %}

    {% if not isEndBlock %}
        {# do things #}
    {% endif %}
{% endfor %}
</code></pre>

<p>Another approach would be to change / override the default criteria of the Matrix field’s ElementCriteriaModel.</p>

<pre><code>{% set blocksCount = entry.myMatrixField.count() %}
{% set isValid = blocksCount - 3 &gt; 0 %}

{% set startBlocks = isValid ? entry.myMatrixField({
    limit: blocksCount - 3,
}) : null %}
</code></pre>

<p>Or criteria to get the last 3 blocks:</p>

<pre><code>{% set endBlocks = entry.myMatrixField({
    order: 'matrixblocks.sortOrder desc',
    limit: 3,
}) %}
</code></pre>

CC BY-SA 3.0
2017-11-22T10:27:02.537Z
2017-11-22T11:03:12.420Z
2017-11-22T11:03:12.420Z
carlcs
<p>You can fetch all elements and slice the last 3 elements from them</p>

<pre><code>{% set entriesList = entry.fieldHandle.find() %}
{% set start = entriesList|length - 3 %}
{% set end = entriesList|length %}
{% set lastThree = entriesList | slice(start, end) %}
</code></pre>

<p>Of course you need to make sure you have at least 3 objects your array</p>

CC BY-SA 3.0
2017-11-22T10:00:10.847Z
2017-11-22T10:00:10.847Z
Robin Schambach
<p>I would like to include a file on every block but the last three. How can I accomplish this?</p>

<templating><matrix>
Get the three last Matrix blocks
# Get the three last Matrix blocks

(Asked by: username_0 on 2017-11-22)

I would like to include a file on every block but the last three. How can I accomplish this?

**Tags:** templating, matrix


---

## 2 answers

### Answer by username_1 on 2017-11-22. Score: 2 | Accepted answer

Inside of a for-loop you have access to some special variables.
{% for block in entry.myMatrixField %}
    {% set isEndBlock = loop.revindex <= 3 %}

    {% if not isEndBlock %}
        {# do things #}
    {% endif %}
{% endfor %}

Another approach would be to change / override the default criteria of the Matrix field’s ElementCriteriaModel.
{% set blocksCount = entry.myMatrixField.count() %}
{% set isValid = blocksCount - 3 > 0 %}

{% set startBlocks = isValid ? entry.myMatrixField({
    limit: blocksCount - 3,
}) : null %}

Or criteria to get the last 3 blocks:
{% set endBlocks = entry.myMatrixField({
    order: 'matrixblocks.sortOrder desc',
    limit: 3,
}) %}


---

### Answer by username_2 on 2017-11-22. Score: 2

You can fetch all elements and slice the last 3 elements from them
{% set entriesList = entry.fieldHandle.find() %}
{% set start = entriesList|length - 3 %}
{% set end = entriesList|length %}
{% set lastThree = entriesList | slice(start, end) %}

Of course you need to make sure you have at least 3 objects your array


---

