2

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

8
  • Have you tried to use the correct syntax? {% set prev = collection.getPrev() %} Not sure what the previous element should be to be honest Commented May 9, 2018 at 12:50
  • Thanks for replying, however when I try to take the getPrev of the collection, my variable seems to be empty.
    – Stef Dg
    Commented May 9, 2018 at 12:54
  • You need to include your criteria as a parameter to the function, otherwise it returns always null. docs.craftcms.com/api/v2/models/… in your case craft.entries.id(2).first().projectList.type('project').relatedTo(entry) Commented May 9, 2018 at 12:56
  • 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
    Commented May 9, 2018 at 13:01
  • pls try {% set prev = collection.getPrev(craft.entries.id(2).first().projectList.type('project').relatedTo(entry)) %} Commented May 9, 2018 at 13:02

1 Answer 1

1

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)  %}
7
  • 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
    Commented May 9, 2018 at 13:51
  • What do you mean by "only get the ID" so far? Commented May 9, 2018 at 13:52
  • 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
    Commented May 9, 2018 at 13:56
  • 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 ElementModeland not a list so looping through it is useless Commented May 9, 2018 at 13:59
  • I'm aware of this, must have worded it wrong.
    – Stef Dg
    Commented May 9, 2018 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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