<p>Based on your example and description, it's a bit hard to tell whether you are dealing with a <code>category</code> template or an <code>entry</code> 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'.</p>

<p>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 <code>relatedTo(category)</code>, and the next and previous categories from <code>category.nextSibling</code> and <code>category.previousSibling</code> (or <code>category.next</code> and <code>category.previous</code> depending on whether your categories are hierarchical). If so, then the following should work (not tested):</p>

<pre><code>{# category template #}

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

&lt;h1&gt;{{ category.title }}&lt;/h1&gt;
&lt;section class="category-index"&gt;
    {% for entry in entries %}
        &lt;article&gt;
            &lt;h2&gt;{{ entry.title }}&lt;/h2&gt;
            &lt;p&gt;...&lt;/p&gt;
        &lt;/article&gt;
    {% endfor %}
&lt;/section&gt;

{% if prevCategory %}&lt;a href="{{ prevCategory.url }}"&gt;Previous&lt;/a&gt; {% endif %}{% if nextCategory %}&lt;a href="{{ nextCategory.url }}"&gt;Next&lt;/a&gt;{% endif %}
</code></pre>

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

CC BY-SA 3.0
2016-12-29T02:26:28.953Z
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." http://www.colorcomputergames.com/media
DevServe
CC BY-SA 3.0
2016-12-29T04:56:59.310Z
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
CC BY-SA 3.0
2016-12-29T04:58:59.203Z
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
CC BY-SA 3.0
2016-12-27T20:10:16.863Z
2016-12-27T20:16:35.763Z
2016-12-27T20:16:35.763Z
Douglas McDonald
<p>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.</p>

<p>My category handler is called 'media'.</p>

<p>So I might have entries in this category such as :
Time
Life
People</p>

<p>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.</p>

<p>I tried:</p>

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

<p>That didn't work, so I also tried:</p>

<pre><code>{% 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 %}
</code></pre>

<p>But also no good.</p>

CC BY-SA 3.0
2016-12-26T19:35:32.120Z
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
<category><params><previous-next>
Display the next and previous category
# Display the next and previous category

(Asked by: username_0 on 2016-12-26)

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 %}Previous{% endif %}
{% if nextEntry %}Next{% 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.

**Tags:** category, params, previous-next
> **Comments:**
> **username_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)?  

---

## 1 answers

### Answer by username_2 on 2016-12-27. Score: 2

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) %}

{{ category.title }}

    {% for entry in entries %}
        
            {{ entry.title }}
            ...
        
    {% endfor %}


{% if prevCategory %}Previous {% endif %}{% if nextCategory %}Next{% 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.

> **Comments:**
> **username_0**: 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." http://www.colorcomputergames.com/media  
> **username_2**: Categories are reorder-able (and hierarchical) — prevSibling and nextSibling will simply return the previous/next in the order that they are set in.  
> **username_2**: 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.  

---

