Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-09T13:51:48.557Z
Answers.Text: 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.
Answers.UserDisplayName: Stef Dg
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-09T13:52:56.750Z
Answers.Text: What do you mean by "only get the ID" so far?
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-09T13:56:29.037Z
Answers.Text: 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 %}.
Answers.UserDisplayName: Stef Dg
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-09T13:59:37.747Z
Answers.Text: 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
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-09T14:02:40.310Z
Answers.Text: I'm aware of this, must have worded it wrong.
Answers.UserDisplayName: Stef Dg
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-09T14:04:11.453Z
Answers.Text: So you should be aware **all** models have a `__toString` method. You'll see the ID when you do `{{ prev }}`
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-09T14:09:31.017Z
Answers.Text: I'm a dumbass. Forgot to check to make sure to only show when the item isn't NULL.
Answers.UserDisplayName: Stef Dg
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-09T13:15:19.690Z
Answers.LastActivityDate: 2018-05-09T13:15:19.690Z
Answers.UserDisplayName: Robin Schambach
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-09T12:50:36.477Z
Comments.Text: Have you tried to use the correct syntax? `{% set prev = collection.getPrev() %}` Not sure what the previous element should be to be honest
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-09T12:54:33.007Z
Comments.Text: Thanks for replying, however when I try to take the getPrev of the collection, my variable seems to be empty.
Comments.UserDisplayName: Stef Dg
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-09T12:56:48.530Z
Comments.Text: 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)`
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-09T13:01:14.333Z
Comments.Text: 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 %}`
Comments.UserDisplayName: Stef Dg
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-09T13:02:10.623Z
Comments.Text: pls try `{% set prev = collection.getPrev(craft.entries.id(2).first().projectList.type('project').relatedTo(entry)) %}`
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-09T13:04:00.603Z
Comments.Text: 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.
Comments.UserDisplayName: Stef Dg
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-09T13:10:10.097Z
Comments.Text: 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
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-09T13:12:11.080Z
Comments.Text: Oh my bad, the previous entry has to be the previous item in the matrix that is of type('project')
Comments.UserDisplayName: Stef Dg
Tags: <entries><matrix><structure><loop><previous-next>
Title: Previous/Next button to next item in Matrix
ThreadText: # 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 %}
  
      <p>{{project}}</p>
  
  {% 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.  
  
  ---
  
  