1

I am quite new to Craft and I am not sure how to get a prev/next link only for entries with related data in a specific field.

My situation: I have a Structure (let's call it sourceEntry) with an optional field (can be empty or can have related data, let's call this one relatedData). That field is of type entries. So sourceEntry is related to other entries (I'll call them targetEntry).

Assuming I am on the entry's detail page (so, my sourceEntry object is just accessible via entry in my twig template) I would do this to link to previous/next entry:

{% set criteria = {section: 'sourceEntry' } %} {% set prev = entry.getPrev(criteria) %} {% set next = entry.getNext(criteria) %} {# …and then just insert the links… #}

But in my case I need to get the prev/next entry that has set relatedData respectively I'd link to the prev/next entry that is related to targetEntry.

Example:
I have five entries of type sourceEntry: A, B, C, D, E.
The relatedData field for two of them is empty: B, C.
The others are related to posts of type targetEntry: A, D, E.
When loading the detail page for A there should be a next link to D, on detail page for D there should be a next link to E and a prev link to A and so on.

How am I able to achieve this? Thanks in advance!

EDIT:

This is the solution for those who are too lazy to click the link in Bob Olde Hampsink's answer: {% set criteria = {section: 'sourceEntry', relatedTo: craft.users } %} {% set prev = entry.getPrev(criteria) %} {% set next = entry.getNext(criteria) %} {# …and then just insert the links… #} I feel kind of dumb now not even been trying this on my own before (although I knew about relations, but I wasn't sure how to set the criteria object properly). Thanks, Bob Olde Hampsink!

1

Add the relatedTo param to your criteria

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.