1

I have an index of projects that uses a standard entries field to select multiple entries to display and also provides a custom order (different to their Structure order). This is my "Projects" page.

Then each of those entries has its own "Project Details" page. On the Project Detail template, I need to determine the "next" project in the parent page's entries field order in relation to the current entry - and also revert to showing the first project as "next" if the current entry is the last and has no next sibling.

I've tried code like this (where ID "10" is the "Work" page with the entries field, and "projects" is the entries field handle)...

{% set parent = craft.entries().id(10).one() %}
{% set siblings = parent.projects.all() %}
{% set next = siblings.nextSiblingOf(entry).one() %}
{% if next is null %}
  {% set next = parent.projects.one() %}
{% endif %}

The problem here is that siblings is an array and not an element query, and I get this error:

Impossible to invoke a method ("nextSiblingOf") on an array.

Is there a way to do something similar to nextSiblingOf() on an array? Or is there a way to output the entries from that "projects" field as a query so that I can use nextSiblingOf() as I've tried above?

1 Answer 1

5

nextSiblingOf() and related methods will only work with Structure entries. What you need instead is next() / prev(): https://docs.craftcms.com/api/v3/craft-base-element.html#next

(At the time of writing, the 3.x docs seem to be lacking any detail on next/prev methods, but the 2.x docs seem to cover it here: https://craftcms.com/docs/2.x/templating/entrymodel.html#getnext-params)

So in your case it'd look like this:

{% set siblings = craft.entries.id(10).one().projects %}
{% set next = entry.next(siblings) ?? siblings.one() %}

Note the lack of .all() at the end of my first line - next() / prev() take an unexecuted element query as an argument.

1
  • That did the trick. Thank James. Jan 16 at 23:56

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.