<p>A category is an element. So is an entry, and an asset, and a tag, and user. When you relate one element with another you get returned an ElementCriteriaModel in the template. So you need to use its methods to actually get the element data. In this case <code>first()</code>.</p>

<p>So if you know for sure that you only have (and care about) one category in an entry, then you can try this for your params.</p>

<pre><code>{% set params = craft.entries.section('projects').order('title asc').relatedTo(entry.category.first()) %}
</code></pre>

<p>You could also set category way up at the top of the template if you know you'll 
use it multiple times in the template. That way you are only making one database query.</p>

<pre><code>{# At the top of the template #}
{% set category = entry.category.first() %}

{# Somewhat lower to display the category #}
{{ category.title }}

{# even lower to pull the other entries. #}
{% set params = craft.entries.section('projects').order('title asc').relatedTo(category) %}
</code></pre>

CC BY-SA 3.0
2016-01-05T02:20:25.353Z
Thanks Bryan! Makes sense now. The only issue is that while the "Prev" button works consistently, the "Next" button goes through all the entries, in order, of the "photo" category (for instance), then goes onto the entries of the other category "design" and loops them infinitely. I can't seem to make sense of it.
Jeremy P. Beasley
CC BY-SA 3.0
2016-01-04T13:16:46.287Z
2016-01-04T13:16:46.287Z
Bryan Redeagle
<p>My original question <a href="https://craftcms.stackexchange.com/questions/10486/how-to-make-next-prev-links-for-entries-circular-endless">here</a> helped me figure out how to build my URL structure but now the site has introduced two categories: <code>design</code> and <code>photo</code>.</p>

<p>I want the "Prev" and "Next" buttons to navigate back and forth within the category of the current entry.</p>

<p>I know how to get the category of the current entry here: </p>

<pre><code>{% for category in entry.category %}
   {{ category.title }}
{% endfor %}
</code></pre>

<p>...but I don't know how to put <code>category.title</code> into a variable to build my <code>params</code>. This is my best shot at the solution but it's broken and I've given up spinning on it myself :) Any help would be much appreciated.</p>

<pre><code>{% set category = "craft.categories.relatedTo(entry)" %}

{% set params = craft.entries.section('projects').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('projects').first() %}
    {% set nextEntryUrl = firstEntry.url %}
{% endif %}

{% if prevEntry %}
    {% set prevEntryUrl = prevEntry.url %}
{% else %}
    {% set lastEntry = craft.entries.section('projects').last() %}
    {% set prevEntryUrl = lastEntry.url %}
{% endif %}
</code></pre>

<categories><previous-next>
How to fetch Category of current Entry and build "Next / Prev" urls
# How to fetch Category of current Entry and build "Next / Prev" urls

(Asked by: username_0 on 2016-01-04)

My original question here helped me figure out how to build my URL structure but now the site has introduced two categories: design and photo.
I want the "Prev" and "Next" buttons to navigate back and forth within the category of the current entry.
I know how to get the category of the current entry here: 
{% for category in entry.category %}
   {{ category.title }}
{% endfor %}

...but I don't know how to put category.title into a variable to build my params. This is my best shot at the solution but it's broken and I've given up spinning on it myself :) Any help would be much appreciated.
{% set category = "craft.categories.relatedTo(entry)" %}

{% set params = craft.entries.section('projects').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('projects').first() %}
    {% set nextEntryUrl = firstEntry.url %}
{% endif %}

{% if prevEntry %}
    {% set prevEntryUrl = prevEntry.url %}
{% else %}
    {% set lastEntry = craft.entries.section('projects').last() %}
    {% set prevEntryUrl = lastEntry.url %}
{% endif %}

**Tags:** categories, previous-next

---

## 1 answers

### Answer by username_1 on 2016-01-04. Score: 2

A category is an element. So is an entry, and an asset, and a tag, and user. When you relate one element with another you get returned an ElementCriteriaModel in the template. So you need to use its methods to actually get the element data. In this case first().
So if you know for sure that you only have (and care about) one category in an entry, then you can try this for your params.
{% set params = craft.entries.section('projects').order('title asc').relatedTo(entry.category.first()) %}

You could also set category way up at the top of the template if you know you'll 
use it multiple times in the template. That way you are only making one database query.
{# At the top of the template #}
{% set category = entry.category.first() %}

{# Somewhat lower to display the category #}
{{ category.title }}

{# even lower to pull the other entries. #}
{% set params = craft.entries.section('projects').order('title asc').relatedTo(category) %}

> **Comments:**
> **username_0**: Thanks Bryan! Makes sense now. The only issue is that while the "Prev" button works consistently, the "Next" button goes through all the entries, in order, of the "photo" category (for instance), then goes onto the entries of the other category "design" and loops them infinitely. I can't seem to make sense of it.  

---

