next_post_link and previous_post_link in custom post type single page, in same non-custom category

I have a custom post type “realisation”, for which I’ve made a file single-realisation.php. These posts use the non-custom taxonomy “category”. In this page, I use the next_post_link() and previous_post_link() functions, with the in_same_term parameter set to TRUE. I’ve also tried to set the other parameters to ‘ ‘ and ‘category’ (as the site was made prior to 3.8, I believe).

My problem is the category doesn’t seem to be taken into account. The links do appear but will lead to the next or previous realisation posts in order of creation, ignoring the category the current post is in.

Yet everything else concerning the category seems to work. I fetch it using $categorie = get_the_category() and then echo that with $category[0]->name and the category’s name does appear correctly. And this is all inside the loop, of course.

I’ve also tried var_dumping get_next_post() and get_adjacent_post() with in_same_term set to true and it’s always the same result; I get the immediate next realisation post despite the category.

Is there something special I should be doing when I’m trying to use the vanilla categories with custom post types with next_post_link() and previous_post_link()? Perhaps the syntax of the taxonomy parameter?

1 Answer
1

First please check your “custom_post_type” function that you Register Custom Post Type is everything correct like 'taxonomies'=> array( 'category') & 'has_archive' => true, ! If so then you must be put wrong way to display it.

About next_post_link and previous_post_link Please try with like this inside your single-realisation.php file

$prev_post = get_previous_post(); $next_post = get_next_post();    
 if(!empty($prev_post)):
        echo '<a href="'.get_the_permalink($prev_post->ID).'">'.__('Previous','text-domain').'</a>';
  endif;

if(!empty($next_post)):
     echo '<a href="'.get_the_permalink($next_post->ID).'" >'.__('Next','text-domain').'</a>';
endif;

I believe you will get your next & Previous post link form your custom post type also;

Thanks
Msua

Leave a Comment