3

Super simple problem. I'm on a news item page (not a paginated overview page) and want to show a link to the next news item in the Channel; So I can't understand why this is outputting nothing at all:

{{ craft.entries.section('news').nextSiblingOf(entry).title }}

I can't seem to get either of the SiblingOf properties to do anything. What simple mistake am I making?

(I have also tried the following to no avail...)

{{ craft.entries.section('news').nextSiblingOf(entry).first().title }}
Matt Wilcox
  • 3,199
  • 1
  • 14
  • 28

1 Answers1

4

Those parameters are only meant to be used on entries within Structure sections. (Just updated the docs to make that more clear.)

If you want to get the next/previous entries within a Channel section, you can use EntryModel::getNext() and getPrev().

There’s a bit of a trick with those functions though, because “next” and “previous” are ambiguous. (Only consider the entries in a specific channel? Or just the ones with a specific custom field value? etc.) So you need to pass element criteria params into those functions to establish what the full order of entries is.

In your case, sounds like you want the next/previous entries within your news section. I’ll assume you want those entries to be listed by Post Date in descending order (so the latest stories are shown first), and the “Next” entry in this case would be whatever story came before the current one; and the “Previous” story would be whatever came later. (If my assumption is wrong, just change desc to asc in the order param.)

{% set newsCriteria = craft.entries.section('news').order('postDate desc') %}
{% set prevEntry = entry.getPrev(newsCriteria) %}
{% set nextEntry = entry.getNext(newsCriteria) %}

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

{% if nextEntry %}
  <a href="{{ nextEntry.url }}" class="next">{{ nextEntry.title }}</a>
{% endif %}
Brandon Kelly
  • 34,307
  • 2
  • 71
  • 137