0

I have portfolio section that has a ‘portfolio’ category attached to it, which has categories ‘2D’ and ‘3D’.

There are 3 templates:

  1. The index template shows a list of all the entries in the portfolio section.
  2. The categories template shows the entries within a specific category.
  3. The entry template shows a single entry.

On the index page (/portfolio) you can click on 'All', '2D' and '3D' whereby 'All' takes you the index page, and 2D and 3D lead to /portfolio/2D and 3D (the categories template). Click on any item in the list and you go to the entry page.

On the entry template, I have a simple ‘previous entry’ and ‘next entry’ browsing navigation, using:

{% set params = {section: 'portfolio', orderBy: 'postDate desc', limit: null} %}
{% set entry_prev = entry.getPrev(params) %}
{% set entry_next = entry.getNext(params) %}

The thing is, this navigation always browses through all of the entries, regardless of what category an entry is in. And that's fine when you've reached the page from the index page, where all of the entries are listed. But I’m trying to make it so when you click an entry on the categories page, and you are then taken to this entry page, the browsing navigation (previous and next entry) takes into account what category you’re in. I use a simple cookie or getQuery() to set and get this information. That way, when you've clicked on an entry in the list with 2D items, you don't get entries from other categories in the prev/next navigation, but when you've clicked on the same item on the index page, you do get to navigate through all of the entries.

I’ve tried:

{% set params = craft.entries.section('portfolio').relatedTo('2d').orderBy('postDate desc').limit(10).all() %}

But this does not work: the navigation still includes entries that do not have the 2d category.

Anyone have any idea how to achieve this?

1
  • Is this on Craft 2?
    – Oli
    Dec 11, 2019 at 0:04

1 Answer 1

0

Try this

// Assuming you have named portfolio group for 2D and 3D as portfolio

// .first() if craft2, .one() if craft3

{% set category = craft.categories.group('portfolio').title('2D').first() %}
{% set params = craft.entries({
   section: 'portfolio',
   relatedTo: category,
   orderBy: 'postDate desc',
   limit: 10
 }) %}

Ref: https://docs.craftcms.com/v3/dev/element-queries/category-queries.html#example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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