<p>In order to get previous or next elements in Craft you have to pass the current criteria in the elements <code>getPrev</code> function. In your case</p>

<pre><code>{% set criteria  = craft.entries.id(2).first().projectList.type('project')

{% set prev = collection.getPrev(criteria)  %}
</code></pre>

CC BY-SA 4.0
2018-05-09T13:51:48.557Z
I have the feeling we're getting closer. I'm now able to show an entire list of projects, with above them the id of the previous project. However I only get the id so far.
Stef Dg
CC BY-SA 4.0
2018-05-09T13:52:56.750Z
What do you mean by "only get the ID" so far?
Robin Schambach
CC BY-SA 4.0
2018-05-09T13:56:29.037Z
I can list the entire list of projects in the correct order. But when I {{prev}} it only shows a number. It does however show NULL before the first item, which changes when I try getNext instead which makes the last item NULL. 

When I loop over the {{prev}} variable It returns a couple of numbers and dates and NULLS. this is simply because I'm probably making a mistake in my {% for item in prev %}.
Stef Dg
CC BY-SA 4.0
2018-05-09T13:59:37.747Z
Please read the documentation about templating... `getPrev` returns you the previous element. If your element is the first one there is obviously no previous. If your element is the last in the list it has logically no successor. It only returns one `ElementModel`and not a list so looping through it is useless
Robin Schambach
CC BY-SA 4.0
2018-05-09T14:02:40.310Z
I'm aware of this, must have worded it wrong.
Stef Dg
CC BY-SA 4.0
2018-05-09T14:04:11.453Z
So you should be aware **all** models have a `__toString` method. You'll see the ID when you do `{{ prev }}`
Robin Schambach
CC BY-SA 4.0
2018-05-09T14:09:31.017Z
I'm a dumbass. Forgot to check to make sure to only show when the item isn't NULL.
Stef Dg
CC BY-SA 4.0
2018-05-09T13:15:19.690Z
2018-05-09T13:15:19.690Z
Robin Schambach
<p>I've made a projects structure with inside a couple of projects. On the homepage people can fill in a matrix with either a project or a textblock.</p>

<p>I'm capable of looping through the detail pages in the order that they've been made in the Projects section. I can't however make them loop in the order the client orders his Matrix. I'm capable of finding the current detail page I'm on by using relatedTo(entry) however I'm uncapable of using getPrev / getNext. </p>

<pre><code>{% set source = craft.entries.id(2).first().projectList.type('project').entries.relatedTo(entry) %}

{% for collection in source %}

{% for project in collection.projectEntry %}

    &lt;p&gt;{{project}}&lt;/p&gt;

{% endfor %}

{% endfor %}
</code></pre>

<p>This returns the current project I'm visiting. Even when using getPrev(entry) or getPrev(source) in there it keeps on returning my current project instead of the prev/next</p>

CC BY-SA 4.0
2018-05-09T12:50:36.477Z
Have you tried to use the correct syntax? `{% set prev = collection.getPrev() %}` Not sure what the previous element should be to be honest
Robin Schambach
CC BY-SA 4.0
2018-05-09T12:54:33.007Z
Thanks for replying, however when I try to take the getPrev of the collection, my variable seems to be empty.
Stef Dg
CC BY-SA 4.0
2018-05-09T12:56:48.530Z
You need to include your criteria as a parameter to the function, otherwise it returns always null. https://docs.craftcms.com/api/v2/models/BaseElementModel.html#getPrev-detail in your case `craft.entries.id(2).first().projectList.type('project').relatedTo(entry)`
Robin Schambach
CC BY-SA 4.0
2018-05-09T13:01:14.333Z
I did however my reply still is only the project i'm currently on.

`{% set source = craft.entries.id(2).first().projectList.type('project').entries.relatedTo(entry) %}`

`{% set previous = source.getPrev() %}`

`{% for collection in previous %}`

 `{% for project in collection.projectEntry %}`

  `<p>{{project}}</p>`

 `{% endfor %}`

`{% endfor %}`
Stef Dg
CC BY-SA 4.0
2018-05-09T13:02:10.623Z
pls try `{% set prev = collection.getPrev(craft.entries.id(2).first().projectList.type('project').relatedTo(entry)) %}`
Robin Schambach
CC BY-SA 4.0
2018-05-09T13:04:00.603Z
After trying your method, my prev keeps returning NULL. When I dont add the relatedTo to my source i'm getting the entire list in the order I want.
Stef Dg
CC BY-SA 4.0
2018-05-09T13:10:10.097Z
Thats why my first question was "not sure what the previous element should be". If you want to have the first block regardless of the current entry you need to remove it from the criteria
Robin Schambach
CC BY-SA 4.0
2018-05-09T13:12:11.080Z
Oh my bad, the previous entry has to be the previous item in the matrix that is of type('project')
Stef Dg
<entries><matrix><structure><loop><previous-next>
Previous/Next button to next item in Matrix
# Previous/Next button to next item in Matrix

(Asked by: username_0 on 2018-05-09)

I've made a projects structure with inside a couple of projects. On the homepage people can fill in a matrix with either a project or a textblock.
I'm capable of looping through the detail pages in the order that they've been made in the Projects section. I can't however make them loop in the order the client orders his Matrix. I'm capable of finding the current detail page I'm on by using relatedTo(entry) however I'm uncapable of using getPrev / getNext. 
{% set source = craft.entries.id(2).first().projectList.type('project').entries.relatedTo(entry) %}

{% for collection in source %}

{% for project in collection.projectEntry %}

    {{project}}

{% endfor %}

{% endfor %}

This returns the current project I'm visiting. Even when using getPrev(entry) or getPrev(source) in there it keeps on returning my current project instead of the prev/next

**Tags:** entries, matrix, structure, loop, previous-next

> **Comments:**
> **username_1**: Have you tried to use the correct syntax? `{% set prev = collection.getPrev() %}` Not sure what the previous element should be to be honest  
> **username_0**: Thanks for replying, however when I try to take the getPrev of the collection, my variable seems to be empty.  
> **username_1**: You need to include your criteria as a parameter to the function, otherwise it returns always null. https://docs.craftcms.com/api/v2/models/BaseElementModel.html#getPrev-detail in your case `craft.entries.id(2).first().projectList.type('project').relatedTo(entry)`  
> **username_0**: I did however my reply still is only the project i'm currently on.  `{% set source = craft.entries.id(2).first().projectList.type('project').entries.relatedTo(entry) %}`  `{% set previous = source.getPrev() %}`  `{% for collection in previous %}`   `{% for project in collection.projectEntry %}`    `<p>{{project}}</p>`   `{% endfor %}`  `{% endfor %}`  
> **username_1**: pls try `{% set prev = collection.getPrev(craft.entries.id(2).first().projectList.type('project').relatedTo(entry)) %}`  
> **username_0**: After trying your method, my prev keeps returning NULL. When I dont add the relatedTo to my source i'm getting the entire list in the order I want.  
> **username_1**: Thats why my first question was "not sure what the previous element should be". If you want to have the first block regardless of the current entry you need to remove it from the criteria  
> **username_0**: Oh my bad, the previous entry has to be the previous item in the matrix that is of type('project')  

---

## 1 answers

### Answer by username_1 on 2018-05-09. Score: 1 | Accepted answer

In order to get previous or next elements in Craft you have to pass the current criteria in the elements getPrev function. In your case
{% set criteria  = craft.entries.id(2).first().projectList.type('project')

{% set prev = collection.getPrev(criteria)  %}

> **Comments:**
> **username_0**: I have the feeling we're getting closer. I'm now able to show an entire list of projects, with above them the id of the previous project. However I only get the id so far.  
> **username_1**: What do you mean by "only get the ID" so far?  
> **username_0**: I can list the entire list of projects in the correct order. But when I {{prev}} it only shows a number. It does however show NULL before the first item, which changes when I try getNext instead which makes the last item NULL.   When I loop over the {{prev}} variable It returns a couple of numbers and dates and NULLS. this is simply because I'm probably making a mistake in my {% for item in prev %}.  
> **username_1**: Please read the documentation about templating... `getPrev` returns you the previous element. If your element is the first one there is obviously no previous. If your element is the last in the list it has logically no successor. It only returns one `ElementModel`and not a list so looping through it is useless  
> **username_0**: I'm aware of this, must have worded it wrong.  
> **username_1**: So you should be aware **all** models have a `__toString` method. You'll see the ID when you do `{{ prev }}`  
> **username_0**: I'm a dumbass. Forgot to check to make sure to only show when the item isn't NULL.  

---

