4

I have an entries field type associated with a structure section page. This displays the entries on this page in a particular order. When you click an entry link ti will take you to a detailed page for more information about that entry. What I'd like to be able to do is paginate through these entries in the order set in the entries field, not just paginate through the entries in asc/desc or alphabetical order as would happen if I just paginated through the channel section that stores the entries.

I tried straight forward pagination as detailed in the documentation but two problems occurred. First the pagination links were created with proper beginning end and number but when clicking through them you remained on the same entry. Second, a pagination URI was appended to the URL which I don't want.

Is pagination even the proper approach to this problem or is there a better solution?

  • When you say you want to “paginate” through them, do you mean you want to list some number of them at a time (pagination), or just show links to the previous and next entries? – Brandon Kelly Jun 13 '14 at 16:11
  • The latter. I started working through the advice below and am halfway through the solution, Just trying to figure out how to sort these by the entries field instead of relying on the order parameter. – Stuart McCoy Jun 13 '14 at 16:17
  • Stuart, you should just change title in the parameters to the field you'd like your entries to be order by. (I am referring to this answer) – carlcs Jun 13 '14 at 16:38
  • It doesn't appear to be that simple. I tried that first thing and got a CDbException error. Looking into relatedTo and seeing if I can maybe build this within an entries loop instead. – Stuart McCoy Jun 13 '14 at 16:58
  • Sorry, I probably didn't read your question thoroughly. Chances are that @Brandon answer is what you are after. Deleting mine. – carlcs Jun 13 '14 at 17:23
4

To do this, you would need to use getPrev() and getNext() on the related entries’ template.

The trick is telling getPrev() and getNext() to use the order you defined in the source entry’s Entries field.

So first, you will need to get that source entry. If you only have a single entry in your structure section that has one of these Entries fields, that could be as simple as this:

{# Get the entry in the structure section that relates to `entry` %}
{% set sourceEntry = craft.entries.section('myStructureSection').relatedTo({
    targetElement: entry,
    field: 'myEntriesField'
}) %}

Once you have that, you can just pass its Entries field into getPrev() and getNext():

{% set prevEntry = entry.getPrev(sourceEntry.myEntriesField) %}
{% set nextEntry = entry.getNext(sourceEntry.myEntriesField) %}

{% if prevEntry %}
    <a href="{{ prevEntry.url }}">Previous: {{ prevEntry.title }}</a>
{% endif %}

{% if nextEntry %}
    <a href="{{ nextEntry.url }}">Next: {{ nextEntry.title }}</a>
{% endif %}
  • This is close but not quite there. I'm going to hack away at it and see if I can figure out why. I might rethink the way this is setup and just use a Structure for sorting purposes instead of a relationship. – Stuart McCoy Jun 13 '14 at 23:43

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.