0

The examples I have seen to link to previous and next entry all seem to be for sections. I want to get the previous and next category.

My category handler is called 'media'.

So I might have entries in this category such as : Time Life People

When viewing the entry 'Life', I want to be able to go back to Time (Previous) or to People (Next). Again, these entries are entries in my category.

I tried:

{% set params = craft.entries.section('media').relatedTo(category) %}
{% set prevEntry = entry.getPrev(params) %}
{% set nextEntry = entry.getNext(params) %}
{% if prevEntry %}<a href="{{ prevEntry.url }}">Previous</a>{% endif %}
{% if nextEntry %}<a href="{{ nextEntry.url }}">Next</a>{% endif %}

That didn't work, so I also tried:

{% set category = entry.category.first() %}
{% set params = craft.entries.section('media').order('title asc').relatedTo(category) %}
{% set prevEntry = entry.getPrev(params) %}
{% set nextEntry = entry.getNext(params) %}
{% if nextEntry %}
    {% set nextEntryUrl = nextEntry.url %}
{% else %}
    {% set firstEntry = craft.entries.section('media').first() %}
    {% set nextEntryUrl = firstEntry.url %}
{% endif %}
{% if prevEntry %}
    {% set prevEntryUrl = prevEntry.url %}
{% else %}
    {% set lastEntry = craft.entries.section('media').last() %}
    {% set prevEntryUrl = lastEntry.url %}
{% endif %}

But also no good.

  • 1
    Assuming category is a valid CategoryModel, and entry belongs to a section with the handle "media", your first example looks legit. In other words, this is pretty much impossible to answer. Maybe share your entire template (as a Github gist, if its big)? – Mats Mikkel Rummelhoff Dec 26 '16 at 19:35
1

Based on your example and description, it's a bit hard to tell whether you are dealing with a category template or an entry template, because in your description you say that the category handle is called 'media', but in the example you are grabbing entries in the section called 'media'.

Assuming you are on a category template (i.e. trying to create a category index page for example), then you should be able to just grab the current category from the 'category' variable (provided by craft), entries related to that category using relatedTo(category), and the next and previous categories from category.nextSibling and category.previousSibling (or category.next and category.previous depending on whether your categories are hierarchical). If so, then the following should work (not tested):

{# category template #}

{% set prevCategory = category.prevSibling %}
{% set nextCategory = category.nextSibling %}
{% set entries = craft.entries('mySection').relatedTo(category) %}

<h1>{{ category.title }}</h1>
<section class="category-index">
    {% for entry in entries %}
        <article>
            <h2>{{ entry.title }}</h2>
            <p>...</p>
        </article>
    {% endfor %}
</section>

{% if prevCategory %}<a href="{{ prevCategory.url }}">Previous</a> {% endif %}{% if nextCategory %}<a href="{{ nextCategory.url }}">Next</a>{% endif %}

If you are dealing with an entry template (i.e. an entry detail page), then you may need to clarify the question a bit.

| improve this answer | |
  • This would be on an entry template. I added your code. It allowed me to go Previous and Next, but it seems like the order was based on the order ID (when I added the category), rather than in alphabetical order. So if we are on category entry "68 Color Micro Journal", the next one should be "68 Micro Journal." colorcomputergames.com/media – DevServe Dec 29 '16 at 2:26
  • Categories are reorder-able (and hierarchical) — prevSibling and nextSibling will simply return the previous/next in the order that they are set in. – Douglas McDonald Dec 29 '16 at 4:56
  • If this is an entry template (i.e. as in an entry in a section or channel), then wouldn't you want next/previous to go to the next entry? vs next category? You may need to clarify the original question a bit more. – Douglas McDonald Dec 29 '16 at 4:58

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.