<p>You can limit the prev/next links to only show entries with the same tags using the <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param"><code>relatedTo</code> param</a>:</p>

<pre><code>{% set params = {
    section: 'databytes',
    order: 'title',
    relatedTo: { targetElement: entry.tagsFieldHandle, field: 'tagsFieldHandle' }
} %}
</code></pre>

<p>That might do the trick, but it may lead to inconsistent prev/next page links, because you can have more that one tag associated with each entry.</p>

<p>Take this entry/tag setup, for example:</p>

<ul>
<li>Pizza
<ul>
<li>Lunch</li>
<li>Dinner</li>
</ul></li>
<li>Ice Cream
<ul>
<li>Desert</li>
</ul></li>
<li>Crepe
<ul>
<li>Breakfast</li>
<li>Lunch</li>
<li>Desert</li>
</ul></li>
</ul>

<p>If you land on Pizza, its <em>Next Page</em> link would take you to Crepe, since they share the Lunch tag. But when you click on Crepe, its <em>Prev Page</em> link should take you to Ice Cream, rather than Pizza, because Crepe also shares the Desert tag with Ice Cream.</p>

<p>You can see why that might get a little confusing.</p>

<p>One way to keep the navigation more consistent would be to choose one single tag that all entries should have in common, and pass that tag in the Prev/Next URLs.</p>

<pre><code>{#
  Figure out which tag ID we should use to limit the previous/next entries.
  It will either be set in the query string already,
  or we can just use the first tag that the current entry has.
#}

    {% set tagId = craft.request.getQuery('tag') %}

    {% if not tagId %}
        {% set tagId = entry.tagsFieldHandle.ids()|first %}
    {% endif %}

{# Find the previous/next entries that have that tag #}

    {% set params = {
        section: 'databytes',
        order: 'title',
        relatedTo: { targetElement: tagId, field: 'tagsFieldHandle' }
    } %}

    {% set prevDatabyte = entry.getPrev(params) %}
    {% set nextDatabyte = entry.getNext(params) %}

{# Output their links, and pass the tag ID in the query string #}

    {% if prevDatabyte %}
        &lt;a href="{{ prevDatabyte.url }}?tag={{ tagId }}" ... /&gt;
    {% endif %}

    {% if nextDatabyte %}
        &lt;a href="{{ nextDatabyte.url }}?tag={{ tagId }}" ... /&gt;
    {% endif %}
</code></pre>

CC BY-SA 3.0
2014-07-03T14:15:15.533Z
2014-07-03T14:15:15.533Z
Brandon Kelly
<p>On a single entries page I have the following code:</p>

<pre><code>&lt;div class="article-pagination"&gt;

    {% set params = {section: 'databytes', order: 'title'} %}
    {% set prevDatabyte = entry.getPrev(params) %}
    {% set nextDatabyte = entry.getNext(params) %}

    {% if prevDatabyte %}
    &lt;a href="{{ prevDatabyte.url }}" data-hint="Previous Databyte" class="prev article-pagination-link icon-alone hint--right"&gt;
      &lt;i class="icon-arrow2-left"&gt;&lt;/i&gt;
    &lt;/a&gt;
    {% endif %}

    {% if nextDatabyte %}
    &lt;a href="{{ nextDatabyte.url }}" data-hint="Next Databyte" class="next article-pagination-link icon-alone hint--left"&gt;
      &lt;i class="icon-arrow2-right"&gt;&lt;/i&gt;
    &lt;/a&gt;
    {% endif %}
&lt;/div&gt;&lt;!-- END .article-pagination --&gt;
</code></pre>

<p>I am wanting to somehow only paginate through entries that are tagged with the same tag maybe by passing that into the params object is that possible?</p>

<pagination>
Paginate Entries With The Same Tag
# Paginate Entries With The Same Tag

(Asked by: username_0 on 2014-07-03)

On a single entries page I have the following code:


    {% set params = {section: 'databytes', order: 'title'} %}
    {% set prevDatabyte = entry.getPrev(params) %}
    {% set nextDatabyte = entry.getNext(params) %}

    {% if prevDatabyte %}
    
      
    
    {% endif %}

    {% if nextDatabyte %}
    
      
    
    {% endif %}


I am wanting to somehow only paginate through entries that are tagged with the same tag maybe by passing that into the params object is that possible?

**Tags:** pagination


---

## 1 answers

### Answer by username_1 on 2014-07-03. Score: 5 | Accepted answer

You can limit the prev/next links to only show entries with the same tags using the relatedTo param:
{% set params = {
    section: 'databytes',
    order: 'title',
    relatedTo: { targetElement: entry.tagsFieldHandle, field: 'tagsFieldHandle' }
} %}

That might do the trick, but it may lead to inconsistent prev/next page links, because you can have more that one tag associated with each entry.
Take this entry/tag setup, for example:

Pizza

Lunch
Dinner

Ice Cream

Desert

Crepe

Breakfast
Lunch
Desert


If you land on Pizza, its Next Page link would take you to Crepe, since they share the Lunch tag. But when you click on Crepe, its Prev Page link should take you to Ice Cream, rather than Pizza, because Crepe also shares the Desert tag with Ice Cream.
You can see why that might get a little confusing.
One way to keep the navigation more consistent would be to choose one single tag that all entries should have in common, and pass that tag in the Prev/Next URLs.
{#
  Figure out which tag ID we should use to limit the previous/next entries.
  It will either be set in the query string already,
  or we can just use the first tag that the current entry has.
#}

    {% set tagId = craft.request.getQuery('tag') %}

    {% if not tagId %}
        {% set tagId = entry.tagsFieldHandle.ids()|first %}
    {% endif %}

{# Find the previous/next entries that have that tag #}

    {% set params = {
        section: 'databytes',
        order: 'title',
        relatedTo: { targetElement: tagId, field: 'tagsFieldHandle' }
    } %}

    {% set prevDatabyte = entry.getPrev(params) %}
    {% set nextDatabyte = entry.getNext(params) %}

{# Output their links, and pass the tag ID in the query string #}

    {% if prevDatabyte %}
        
    {% endif %}

    {% if nextDatabyte %}
        
    {% endif %}


---

