I am using this code, but once I click on the next post/previous post link, I am redirected to the next post/previous post of a different category.
previous_post_link( '%link', 'Prev post in category', $in_same_term = true );
next_post_link( '%link', 'Next post in category', $in_same_term = true );
I am trying to fix my issue using this article.
Here is the code I am using for posts and categories, and I am not using custom post types and categories:
$post_id = $post->ID;
$cat = get_the_category();
$current_cat_id = $cat[0]->cat_ID;
$args = array(
'category' => $current_cat_id,
'orderby' => 'post_date',
'order' => 'DESC'
);
$posts = get_posts( $args );
foreach( $posts as $post ) {
echo $post->post_content;
}
previous_post_link( '%link', 'Prev post in category', $in_same_term = true );
next_post_link( '%link', 'Next post in category', $in_same_term = true );
It's basically fetching all the posts based on the category, and now I want the next post/ previous post link to work only for this particular category.
$posts
array from the code you provided, or from the main post on the current page?