4

There are some great options to do prev/next entries (Advanced Prev/Next, Nearby Entries, etc.) but I could not find an add-on or an EE-native way to do a "Go to Next Category" and/or "Go to Previous Category".

Am I missing something obvious?

2 Answers 2

3

As mentioned, you can easily do this with the query module. I've done something very similar, but you could pass the needed variables to an embed to get the prev/next category.

[removed] see updated answer below

Update

Depending on how your categories are ordered (alphabetical or custom) & nested a few more variables are needed. You will need to use an add-on like GWcode Categories to get the necessary variables for a query, specifically cat_order, which GWcode gets.

Parent Template:

{exp:gwcode_categories
    cat_url_title="{segment_3}"
    limit="1"
}
    {embed="some/template" cat_url_title="{cat_url_title}" group_id="{cat_group_id}" parent_id="{parent_id}" cat_order="{cat_order}"}
{/exp:gwcode_categories}

Embded

{exp:query sql=
"SELECT 
    cat_url_title as prev_cat_url_title, 
    cat_name as prev_cat_title
FROM exp_categories 
WHERE cat_url_title <> '{embed:cat_url_title}'
AND parent_id = {embed:parent_id}
AND group_id = {embed:group_id}
AND cat_order = {emebd:cat_order} - 1
LIMIT 1"
}
<a href="../{prev_cat_url_title}/">{prev_cat_title}</a>
{/exp:query}

For next category, change the cat_order clause to:

AND cat_order = {emebd:cat_order} + 1
7
  • 1
    If you have parent/child cats or instances where cat_order is equal across multiple categories, SQL won't suffice. Your query isn't checking cat_order either. Jan 24, 2013 at 23:30
  • 1
    He never mentioned what the requirement for prev/next category. So if it is custom, then this wouldn't work, if it's alphabetical, then it will. So to vote this answer down doesn't make sense to me.
    – Siebird
    Jan 25, 2013 at 0:29
  • 1
    EE supports ordering categories and parent/child categories. That is core functionality, not a custom requirement. EE's default category output is not alphabetical. Jan 25, 2013 at 1:27
  • 1
    Not entirely true. It allows sorting alphabetically or custom, see the footer any category group: Master Sort Order
    – Siebird
    Jan 25, 2013 at 13:12
  • 1
    I like the update and incorporation of cat_order. There are still a few odd cases that may not cover: I see instances in my database where children of the same parent category have the same cat_order value. To match EE's ordering, you'd then have to then switch to, or incorporate, order by Title. If you "Next" through all children, what is the next category? Is it the next parent category in the list? This would assume you're only cycling through the current level. This is why I recommended a non-SQL approach. Jan 25, 2013 at 13:57
1

I wrote some code a while back that stepped through categories to generate lists the way I wanted them. While {exp:query} is the go-to solution for a lot of category interaction, you can't just use SQL to get these, you need to follow the logic that EE uses to order them.

You can check out the recursive subcategories add-on on Github if you want to try to whip up a plugin that does the same. There's a link to a fork that ports it to EE2.

Your Answer

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

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