2

I have an entry which needs to display the previous two entries, and the next two. I see in the docs there's a nextSiblingOf method. That does nothing in a channel, and in a structure it will only return one entry. I can't see an ability to "offset" the result.

1: Is this possible in a channel

2: What do I need to do to grab the previous/next two entries in either case?

4

In a channel this would be the next entries where the post date is before and after the current entry’s post date. The element queries for it would look like so:

{% set query = craft.entries
    .section('blog')
    .id('not ' ~ entry.id)
    .limit(2) %}

{% set prevEntries = clone(query)
    .postDate('<= ' ~ entry.postDate.timestamp)
    .order('postDate desc')
    .all() %}

{% set nextEntries = clone(query)
    .postDate('>= ' ~ entry.postDate.timestamp)
    .order('postDate asc')
    .all() %}

If your sort the entries by title, something like this should work:

{% set prevEntries = clone(query)
    .title('<= ' ~ entry.title)
    .order('title desc')
    .all() %}

{% set nextEntries = clone(query)
    .title('>= ' ~ entry.title)
    .order('title asc')
    .all() %}
2
  • Thanks Christian - looks like maybe I should change the type. The primary way this data is going to be displayed is alphabetically. For some reason I thought "It should be a structure, because they won't be able to re-order them", but perhaps it should be a Structure after all? Aug 13 '18 at 12:32
  • A channel is probably the right section type if you don’t need manual sort order. I just updated the answer and added an example for alphabetically ordered entries.
    – carlcs
    Aug 13 '18 at 12:42

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.