Entry Pagination - Group by Current Category and Order by Post Date 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 %}

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


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 &amp; 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 %}

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


Category Template

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