1

I currently have one entry in a page and would like to display next and previous arrows. The next/previous entries should be in the same section and related to the same category. To do this I have written this code:

{% set category = craft.categories.group('seasons').title(entry.season.title).first() %}
{% set arrowsParams = craft.entries.section('blog').relatedTo(category) %}
{% set nextEntry = entry.getNext(arrowParams) %}
{% set prevEntry = entry.getPrev(arrowParams) %}

For each entry I have added a field 'season' which maps to a category.

When retrieving the nextEntry, the relatedTo category is being neglected and an entry in a different category is being returned.

When I do

craft.entries.section('blog').relatedTo(category).first()

it works fine and actually retrieves the first entry in that category.

Am I missing something? Could this be done?

  • 1
    Maybe it's as easy as fixing the typo arrowsParams, shanti? – carlcs Aug 23 '14 at 9:22
  • oohh, that was stupid! – shanti Aug 23 '14 at 9:37
4

The code you currently have (after fixing the arrowsParams typo) will only work on entries that happen to have the very first category in the Seasons category group selected, because this title parameter is actually not getting any value right now:

.title(entry.season.title)

Why? Because entry.season is not going to return the first category you have selected in your Season categories field; it’s going to return an ElementCriteriaModel that is prepped to return the category(s) selected in your Season categories field, once one of its output functions have been called.

Since entry.season is an ElementCriteriaModel, entry.season.title is actually just going to return its current title parameter value (which is null by default). So this:

{% set category = craft.categories.group('seasons').title(entry.season.title).first() %}

is really the same as this:

{% set category = craft.categories.group('seasons').title(null).first() %}

which is the same as not including the parameter at all, so it’s also the same as this:

{% set category = craft.categories.group('seasons').first() %}

So category is always going to be set to the first category in the group, and arrowsParams is going to be set to all of the Blog entries that are related to that first category.

If the entry you’re viewing doesn’t have that first category selected, then its getNext() and getPrev() functions won’t return anything. (If they can’t determine where the current entry fits into the parameters that were passed in, they can’t determine what the next and previous entries would be.)

Anyway, to fix this, you would need to put a .first() call inside that title param when setting the current category:

{% set category = craft.categories.group('seasons').title(entry.season.first().title).first() %}

But here’s the thing: entry.season.first() is already the category you’re trying to get. So there’s no point in fetching it through a second craft.categories query in the first place. This will do just fine:

{% set category = entry.season.first() %}

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.