Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

On a single entries page I have the following code:

<div class="article-pagination">

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

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

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

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?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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 %}
        <a href="{{ prevDatabyte.url }}?tag={{ tagId }}" ... />
    {% endif %}

    {% if nextDatabyte %}
        <a href="{{ nextDatabyte.url }}?tag={{ tagId }}" ... />
    {% endif %}
share|improve this answer
    
Thanks Brandon, I'll give that a try. –  Gareth Redfern Jul 3 at 15:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.