Working on a client project in WordPress recently, I came across something I’d not thought of before…
Category based Previous and Next links on Custom Post Types.
I’d heard of category based links for standard blog post but I’d never actually seen it used. After a bit of Googling and looking back through previous projects I managed to find a solution.
The Use Case
- I have a custom post type called “Products”
- I have a custom taxonomy called “Product Categories”
- I have a bunch of products that I want to link to via product category
- Whilst in this individual product page I want to find out my current custom post type category and display previous and next links to allow me to navigate but only within my current product category
The Code
Here is the code I used to grab the current category and then get the posts based on that category…
<?php // Get terms for post $terms = get_the_terms( $post->ID , 'productcat' ); // Loop over each item since it's an array if ( $terms != null ){ foreach( $terms as $term ) { // Print the name method from $term which is an OBJECT $termSlug = $term->slug; // Get rid of the other data stored in the object, since it's not needed unset($term); } } ?> <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->slug; // will show the slug // get_posts in same custom taxonomy $postlist_args = array( 'posts_per_page' => -1, 'orderby' => 'menu_order title', 'order' => 'ASC', 'post_type' => 'product', // this can be your post type 'productcat' => $termSlug // get slug of product category from above - change productcat for your taxonomy slug ); $postlist = get_posts( $postlist_args ); // get ids of posts retrieved from get_posts $ids = array(); foreach ($postlist as $thepost) { $ids[] = $thepost->ID; } // get and echo previous and next post in the same taxonomy $thisindex = array_search($post->ID, $ids); $previd = $ids[$thisindex-1]; $nextid = $ids[$thisindex+1]; ?>
Then the following to display the above…
<?php if ( !empty($previd) ) { echo '<a rel="prev" href="' . get_permalink($previd). '">previous</a>'; } if ( !empty($nextid) ) { echo '<a rel="next" href="' . get_permalink($nextid). '">next</a>'; } ?>
Ant June 30, 2014 at 2:00 pm
hey Chris, thanks for this – exactly what I needed. One question – doesn’t it significantly slow down your page load? On my site its taking a good few seconds to run the code. How do you get around this?
Chris Wharton July 3, 2014 at 8:48 am
Hi Ant, I’d not noticed any difference in page load at all and that’s on a really low spec cloud hosting account (dev site) and on a dedicated VPS (live site)
Lee October 2, 2014 at 2:57 pm
Cheers for this, finally a solution that actually works! I also used it for general CPT pagination by just removing the taxonomy elements and it seems to work fine
Thanks
Chris Wharton January 29, 2015 at 1:54 pm
Lovely, no problem at all :)
RJ McCollam October 10, 2014 at 4:55 pm
Just wanted to say thank you for this, worked perfectly. Did quite a bit of googling until I got to this.
Chris Wharton January 29, 2015 at 1:54 pm
No problem, happy to help!
mac April 7, 2016 at 10:59 am
Hey Chris, sorry, for asking. I should enter the first code into my functions.php? Is there a possibility to show the title of the prev/next post? Just like %title when using WordPress own next_post_link? Cheers
Chris Wharton April 21, 2016 at 2:29 pm
Hi mac, no the code should go above the next portion of code :)
James July 22, 2017 at 3:13 pm
Hey Chris! Thank you for these codes. I am a newbie and just wanted to make sure I know where to put those two different codes exactly at. Do I add the first portion in functions.php and the second one in index.php ? Thank you.
Chris Wharton August 7, 2017 at 11:07 am
Hi James, no it would all go wherever you call the navigation.
So with this example, the code goes in a file called “_part-nav-product.php” and is reference from the “single-product.php” file as a get_template_part()
Dane August 7, 2017 at 6:12 pm
Hey. I just found this post today. I’m pretty new to this and I’m trying out my first Child Theme. In order to get this to work, do I need to create a new document in my Child Theme called “_part-nav-product.php” or do I need to find out where Divi calls navigation and name my Child Theme navigation file whatever that is?
Chris Wharton August 8, 2017 at 11:30 am
Hi Dane, have you got a custom post type you are using?
Jack Morrell November 1, 2016 at 4:23 pm
Cheers Chris! I spent hours looking for a solution like this, so thanks for posting.
Chris Wharton November 2, 2016 at 2:30 pm
Yay! Glad I could help :)
German February 1, 2019 at 10:06 pm
Thanks for your tutorial, helped me a lot…