<p>Using the <a href="https://craftcms.com/docs/templating/paginate" rel="nofollow">pagination helper</a> might be a cleaner way to achieve your desired results. It would go something like this:</p>

<pre><code>{% paginate craft.entries.section('news').order('postDate').relatedTo({targetElement:categoryId}).limit(5) as entries %}
    {% for item in entries %}
        {# your item code goes here #}
    {% endfor %}
    {% if paginate.prevUrl %}
        &lt;a class="button left" href="{{ paginate.prevUrl }}"&gt;Previous&lt;/a&gt;
    {% endif %}
    {% if paginate.nextUrl %}
        &lt;a class="button right" href="{{ paginate.nextUrl }}"&gt;Next&lt;/a&gt;
    {% endif %}
{% endpaginate %}
</code></pre>

<p>By using the <code>relatedTo({targetElement:categoryId})</code> param you will stay in the same category as you paginate entries. This pagination helper handles the urls.  So we are just accessing the <code>paginate.prevUrl &amp; paginate.nextUrl</code> variables provided by the helper.  All you need to do is make sure the categoryId is set to the category you need and set the limit to the number of items to paginate by.</p>

<p>--</p>

<p>For your particular setup I would use two templates.  One for the News landing page and one for the category landing. The news landing page will paginate all News entries. </p>

<p>Once a category is clicked and now uses the template specified in the category setup page, you will limit paginated entries to the current category.  The variable <code>category</code> is reserved for those templates so you can access anything variable associated with the current category you are on. You will only notice slight difference between the two. The Category Template adds the <code>relatedTo({targetElement:category.id})</code> criteria in addition to setting the active category class.</p>

<p>Here are both templates.</p>

<p><strong>News Landing Template</strong></p>

<pre><code>{% set title = entry.title %}
{% set description = entry.body | striptags %}

{% extends "_layout" %}

{% block content %}

  &lt;div class="heading"&gt;
    &lt;h1&gt;{{ newsHeader.header }}&lt;/h1&gt;
  &lt;/div&gt;

  &lt;div class="filter-bar news"&gt;
      &lt;div class="container"&gt;
        &lt;ul class="largenav"&gt;
            &lt;li&gt;&lt;a href="/news"&gt;All&lt;/a&gt;&lt;/li&gt;
            {% for topic in craft.categories.group('newsCategories') %}
              &lt;li&gt;
                &lt;a href="{{ topic.url }}"&gt;{{ topic.title }}&lt;/a&gt;
              &lt;/li&gt;
            {% endfor %}
        &lt;/ul&gt;
        &lt;div class="select tinynav"&gt;
            &lt;a href="#" id="select-toggle"&gt;All&lt;/a&gt;
            &lt;ul&gt;
                &lt;li&gt;&lt;a data-value="{{ siteUrl }}news" href="#"&gt;All&lt;/a&gt;&lt;/li&gt;
                {% for topic in craft.categories.group('newsCategories') %}
                  &lt;li&gt;
                    &lt;a href="#" data-value="{{ topic.url }}"&gt;{{ topic.title }}&lt;/a&gt;
                  &lt;/li&gt;
                {% endfor %}
            &lt;/ul&gt;
        &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;

    {% paginate craft.entries.section('news').order('postDate').limit(5) as entries %}
      &lt;ul class="wrapper newspage"&gt;
        {% for item in entries %}
          &lt;li class="news-item"&gt;
            &lt;h1&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/h1&gt;
              &lt;h5 class="meta"&gt;{{ entry.postDate|date("F j, Y") }} &amp;nbsp;|&amp;nbsp;

                  {% if entryCategories | length %}
                    {% for topic in entryCategories %}
                      &lt;a href="{{ topic.url }}"&gt;{{ topic.title }}&lt;/a&gt;
                    {% endfor %}
                  {% endif %}
              &lt;/h5&gt;
            &lt;p&gt;{{ entry.body }}&lt;/p&gt;
          &lt;/li&gt;  
        {% endfor %}
      &lt;/ul&gt;
      &lt;li class="news-item"&gt;
        {% if paginate.prevUrl %}
            &lt;a class="button left" href="{{ paginate.prevUrl }}"&gt;Previous&lt;/a&gt;
        {% endif %}
        {% if paginate.nextUrl %}
            &lt;a class="button right" href="{{ paginate.nextUrl }}"&gt;Next&lt;/a&gt;
        {% endif %}

        &lt;div class="clear"&gt;&lt;/div&gt;
      &lt;/li&gt;
    {% endpaginate %}
  &lt;/ul&gt;

{% endblock %}
</code></pre>

<p><strong>Category Template</strong></p>

<pre><code>{% set title = entry.title %}
{% set description = entry.body | striptags %}

{% extends "_layout" %}

{% block content %}

  &lt;div class="heading"&gt;
    &lt;h1&gt;{{ newsHeader.header }}&lt;/h1&gt;
  &lt;/div&gt;

  &lt;div class="filter-bar news"&gt;
      &lt;div class="container"&gt;
        &lt;ul class="largenav"&gt;
            &lt;li&gt;&lt;a href="/news"&gt;All&lt;/a&gt;&lt;/li&gt;
            {% for topic in craft.categories.group('newsCategories') %}
              &lt;li&gt;
                &lt;a href="{{ topic.url }}" {% if category.url == topic.url %}class="active"{% endif %}&gt;{{ topic.title }}&lt;/a&gt;
              &lt;/li&gt;
            {% endfor %}
        &lt;/ul&gt;
        &lt;div class="select tinynav"&gt;
            &lt;a href="#" id="select-toggle"&gt;All&lt;/a&gt;
            &lt;ul&gt;
                &lt;li&gt;&lt;a data-value="{{ siteUrl }}news" href="#"&gt;All&lt;/a&gt;&lt;/li&gt;
                {% for topic in craft.categories.group('newsCategories') %}
                  &lt;li&gt;
                    &lt;a href="#" data-value="{{ topic.url }}"&gt;{{ topic.title }}&lt;/a&gt;
                  &lt;/li&gt;
                {% endfor %}
            &lt;/ul&gt;
        &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;

    {% paginate craft.entries.section('news').order('postDate').relatedTo({targetElement:category.id}).limit(5) as entries %}
      &lt;ul class="wrapper newspage"&gt;
        {% for item in entries %}
          &lt;li class="news-item"&gt;
            &lt;h1&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/h1&gt;
              &lt;h5 class="meta"&gt;{{ entry.postDate|date("F j, Y") }} &amp;nbsp;|&amp;nbsp;

                  {% if entryCategories | length %}
                    {% for topic in entryCategories %}
                      &lt;a href="{{ topic.url }}"&gt;{{ topic.title }}&lt;/a&gt;
                    {% endfor %}
                  {% endif %}
              &lt;/h5&gt;
            &lt;p&gt;{{ entry.body }}&lt;/p&gt;
          &lt;/li&gt;  
        {% endfor %}
      &lt;/ul&gt;
      &lt;li class="news-item"&gt;
        {% if paginate.prevUrl %}
            &lt;a class="button left" href="{{ paginate.prevUrl }}"&gt;Previous&lt;/a&gt;
        {% endif %}
        {% if paginate.nextUrl %}
            &lt;a class="button right" href="{{ paginate.nextUrl }}"&gt;Next&lt;/a&gt;
        {% endif %}

        &lt;div class="clear"&gt;&lt;/div&gt;
      &lt;/li&gt;
    {% endpaginate %}
  &lt;/ul&gt;

{% endblock %}
</code></pre>

CC BY-SA 3.0
2016-01-05T13:35:15.140Z
Hi @aran, thanks for your help. I tried this using this code while replacing "categoryID" with one of the category ID's, and it didn't really work. When you click Next or Previous, it just goes to the same post over and over again. Is there a way to create a variable that gets the current category ID of the post the user is on, and use a variable instead of a static ID?

Here is the page: http://monderer.com/news/announcements
Charlotte H
CC BY-SA 3.0
2016-01-05T15:25:36.353Z
Are the sub navigation items categories, and when clicked they take you to the detail page route for the category?
a-am
CC BY-SA 3.0
2016-01-05T16:39:48.717Z
the sub navigation items are categories, and they just take you to a list of posts assigned to that category. We just want users to remain in the chosen category when navigating with the Next and Previous buttons.
Charlotte H
CC BY-SA 3.0
2016-01-05T18:52:30.323Z
Then when you are on at a category url you can use `category.id` to get the current category your on.  So it would be `{targetElement:category.id}`.  Then your paginate helper should always stay in that category.  If this doesn't seem to work for you please post your code and I will take a look.
a-am
CC BY-SA 3.0
2016-01-07T13:06:57.633Z
Hi @aran, I've changed my question to contain all of the code from the page so you can see what I mean.
Charlotte H
CC BY-SA 3.0
2016-01-07T16:01:08.210Z
@CharlotteH I have updated my answer as well.
a-am
CC BY-SA 3.0
2016-01-04T21:02:00.187Z
2016-01-07T16:00:43.567Z
2016-01-07T16:00:43.567Z
a-am
<p>I'm new to Craft and am trying to have our entry pagination (next/prev) links go to the next entry within that same category, ordering by date.</p>

<p>So the order part is working as I want it to, it's just remaining in the current category I need help with. Thanks!</p>

<p>Here's what I have so far...</p>

<pre><code>{% set title = entry.title %}
{% set description = entry.body | striptags %}

{% extends "_layout" %}

{% block content %}

{% set entryCategories = entry.newsCategories %}

{% set thisCategory = '' %}

{% for category in entryCategories %}

{% set thisCategory = category.url %}

{% endfor %}

  &lt;div class="heading"&gt;
    &lt;h1&gt;{{ newsHeader.header }}&lt;/h1&gt;
  &lt;/div&gt;
  &lt;div class="filter-bar news"&gt;
      &lt;div class="container"&gt;
        &lt;ul class="largenav"&gt;
            &lt;li&gt;&lt;a href="/news"&gt;All&lt;/a&gt;&lt;/li&gt;{% nav category in     craft.categories.group('newsCategories') %}{% if category.url == thisCategory %}&lt;li class="active"&gt;&lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;&lt;/li&gt;{% else %}&lt;li&gt;&lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;&lt;/li&gt;{% endif %}{% endnav %}
        &lt;/ul&gt;
        &lt;div class="select tinynav"&gt;
            &lt;a href="#" id="select-toggle"&gt;All&lt;/a&gt;
            &lt;ul&gt;
                &lt;li&gt;&lt;a data-value="{{ siteUrl }}news" href="#"&gt;All&lt;/a&gt;&lt;/li&gt;
                {% nav category in craft.categories.group('newsCategories') %}
                &lt;li&gt;&lt;a href="#" data-value="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;&lt;/li&gt;
                {% endnav %}
        &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;



  &lt;ul class="wrapper newspage"&gt;

        &lt;ul&gt;
        &lt;li class="news-item"&gt;
          &lt;h1&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/h1&gt;
          &lt;h5 class="meta"&gt;{{ entry.postDate|date("F j, Y") }} &amp;nbsp;|&amp;nbsp;

              {% if entryCategories | length %}

              {% for category in entryCategories %}

                &lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;

              {% endfor %}
              {% endif %}

            &lt;/h5&gt;
      &lt;p&gt;{{ entry.body }}&lt;/p&gt;

    &lt;/li&gt;
    &lt;/ul&gt;

    &lt;ul&gt;
            &lt;li class="news-item"&gt;

                {# Set parameters for prev/next elements list #}
                {% set params = {section: 'news', category: category.title, order: 'postDate'} %}

                {# Get the prev/next elements #}
                {% set prevEntry = entry.getPrev(params) %}
                {% set nextEntry = entry.getNext(params) %}

                {# And make sure to only output the links if the element exists #}
                {% if prevEntry %}&lt;a class="button left" href="{{ prevEntry.url }}"&gt;Previous&lt;/a&gt;{% endif %}
                {% if nextEntry %}&lt;a class="button right" href="{{ nextEntry.url }}"&gt;Next&lt;/a&gt;{% endif %}

                &lt;div class="clear"&gt;&lt;/div&gt;
            &lt;/li&gt;
      &lt;/ul&gt;


  &lt;/ul&gt;

{% endblock %}
</code></pre>

CC BY-SA 3.0
2016-01-04T19:05:28.180Z
`it's just remaining in the current category I need help with.` - I'm not following that.  What's the behavior you're expecting and what you are currently seeing?
Brad Bell
CC BY-SA 3.0
2016-01-04T20:02:14.227Z
Say you had two categories, Dogs, and Cats. If you were reading a post in the Dog category and clicked Next, it should link to the next post by date within the Dog category. As it is right now, when you click Next, it just goes to the next post by date, without regard for the category.
Charlotte H
CC BY-SA 3.0
2016-01-04T20:38:46.197Z
So there's a category field on the entries and you only want to grab the entries with a specific category selected?
Brad Bell
CC BY-SA 3.0
2016-01-05T13:37:23.277Z
Yes. So you would only navigate through the posts in one category, until you click on a different category to navigate through. Here's the page link if that helps: http://monderer.com/news/announcements
Charlotte H
<pagination><query>
Entry Pagination - Group by Current Category and Order by Post Date
# Entry Pagination - Group by Current Category and Order by Post Date

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

I'm new to Craft and am trying to have our entry pagination (next/prev) links go to the next entry within that same category, ordering by date.
So the order part is working as I want it to, it's just remaining in the current category I need help with. Thanks!
Here's what I have so far...
{% set title = entry.title %}
{% set description = entry.body | striptags %}

{% extends "_layout" %}

{% block content %}

{% set entryCategories = entry.newsCategories %}

{% set thisCategory = '' %}

{% for category in entryCategories %}

{% set thisCategory = category.url %}

{% endfor %}

  
    {{ newsHeader.header }}
  
  
      
        
            All{% nav category in     craft.categories.group('newsCategories') %}{% if category.url == thisCategory %}{{ category.title }}{% else %}{{ category.title }}{% endif %}{% endnav %}
        
        
            All
            
                All
                {% nav category in craft.categories.group('newsCategories') %}
                {{ category.title }}
                {% endnav %}
        
      
  



  

        
        
          {{ entry.title }}
          {{ entry.postDate|date("F j, Y") }}  | 

              {% if entryCategories | length %}

              {% for category in entryCategories %}

                {{ category.title }}

              {% endfor %}
              {% endif %}

            
      {{ entry.body }}

    
    

    
            

                {# Set parameters for prev/next elements list #}
                {% set params = {section: 'news', category: category.title, order: 'postDate'} %}

                {# Get the prev/next elements #}
                {% set prevEntry = entry.getPrev(params) %}
                {% set nextEntry = entry.getNext(params) %}

                {# And make sure to only output the links if the element exists #}
                {% if prevEntry %}Previous{% endif %}
                {% if nextEntry %}Next{% endif %}

                
            
      


  

{% endblock %}

**Tags:** pagination, query

> **Comments:**
> **username_1**: `it's just remaining in the current category I need help with.` - I'm not following that.  What's the behavior you're expecting and what you are currently seeing?  
> **username_0**: Say you had two categories, Dogs, and Cats. If you were reading a post in the Dog category and clicked Next, it should link to the next post by date within the Dog category. As it is right now, when you click Next, it just goes to the next post by date, without regard for the category.  
> **username_1**: So there's a category field on the entries and you only want to grab the entries with a specific category selected?  
> **username_0**: Yes. So you would only navigate through the posts in one category, until you click on a different category to navigate through. Here's the page link if that helps: http://monderer.com/news/announcements  

---

## 1 answers

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

Using the pagination helper might be a cleaner way to achieve your desired results. It would go something like this:
{% paginate craft.entries.section('news').order('postDate').relatedTo({targetElement:categoryId}).limit(5) as entries %}
    {% for item in entries %}
        {# your item code goes here #}
    {% endfor %}
    {% if paginate.prevUrl %}
        Previous
    {% endif %}
    {% if paginate.nextUrl %}
        Next
    {% endif %}
{% endpaginate %}

By using the relatedTo({targetElement:categoryId}) param you will stay in the same category as you paginate entries. This pagination helper handles the urls.  So we are just accessing the paginate.prevUrl & paginate.nextUrl variables provided by the helper.  All you need to do is make sure the categoryId is set to the category you need and set the limit to the number of items to paginate by.
--
For your particular setup I would use two templates.  One for the News landing page and one for the category landing. The news landing page will paginate all News entries. 
Once a category is clicked and now uses the template specified in the category setup page, you will limit paginated entries to the current category.  The variable category is reserved for those templates so you can access anything variable associated with the current category you are on. You will only notice slight difference between the two. The Category Template adds the relatedTo({targetElement:category.id}) criteria in addition to setting the active category class.
Here are both templates.
News Landing Template
{% set title = entry.title %}
{% set description = entry.body | striptags %}

{% extends "_layout" %}

{% block content %}

  
    {{ newsHeader.header }}
  

  
      
        
            All
            {% for topic in craft.categories.group('newsCategories') %}
              
                {{ topic.title }}
              
            {% endfor %}
        
        
            All
            
                All
                {% for topic in craft.categories.group('newsCategories') %}
                  
                    {{ topic.title }}
                  
                {% endfor %}
            
        
      
  

    {% paginate craft.entries.section('news').order('postDate').limit(5) as entries %}
      
        {% for item in entries %}
          
            {{ entry.title }}
              {{ entry.postDate|date("F j, Y") }}  | 

                  {% if entryCategories | length %}
                    {% for topic in entryCategories %}
                      {{ topic.title }}
                    {% endfor %}
                  {% endif %}
              
            {{ entry.body }}
            
        {% endfor %}
      
      
        {% if paginate.prevUrl %}
            Previous
        {% endif %}
        {% if paginate.nextUrl %}
            Next
        {% endif %}

        
      
    {% endpaginate %}
  

{% endblock %}

Category Template
{% set title = entry.title %}
{% set description = entry.body | striptags %}

{% extends "_layout" %}

{% block content %}

  
    {{ newsHeader.header }}
  

  
      
        
            All
            {% for topic in craft.categories.group('newsCategories') %}
              
                {{ topic.title }}
              
            {% endfor %}
        
        
            All
            
                All
                {% for topic in craft.categories.group('newsCategories') %}
                  
                    {{ topic.title }}
                  
                {% endfor %}
            
        
      
  

    {% paginate craft.entries.section('news').order('postDate').relatedTo({targetElement:category.id}).limit(5) as entries %}
      
        {% for item in entries %}
          
            {{ entry.title }}
              {{ entry.postDate|date("F j, Y") }}  | 

                  {% if entryCategories | length %}
                    {% for topic in entryCategories %}
                      {{ topic.title }}
                    {% endfor %}
                  {% endif %}
              
            {{ entry.body }}
            
        {% endfor %}
      
      
        {% if paginate.prevUrl %}
            Previous
        {% endif %}
        {% if paginate.nextUrl %}
            Next
        {% endif %}

        
      
    {% endpaginate %}
  

{% endblock %}

> **Comments:**
> **username_0**: Hi @aran, thanks for your help. I tried this using this code while replacing "categoryID" with one of the category ID's, and it didn't really work. When you click Next or Previous, it just goes to the same post over and over again. Is there a way to create a variable that gets the current category ID of the post the user is on, and use a variable instead of a static ID?  Here is the page: http://monderer.com/news/announcements  
> **username_2**: Are the sub navigation items categories, and when clicked they take you to the detail page route for the category?  
> **username_0**: the sub navigation items are categories, and they just take you to a list of posts assigned to that category. We just want users to remain in the chosen category when navigating with the Next and Previous buttons.  
> **username_2**: Then when you are on at a category url you can use `category.id` to get the current category your on.  So it would be `{targetElement:category.id}`.  Then your paginate helper should always stay in that category.  If this doesn't seem to work for you please post your code and I will take a look.  
> **username_0**: Hi @aran, I've changed my question to contain all of the code from the page so you can see what I mean.  
> **username_2**: @CharlotteH I have updated my answer as well.  

---

