I need display posts on same category in links next and previous posts.
-
I tried this code what I found:
$post_id = $post->ID; // current post id $cat = get_the_category(); $current_cat_id = $cat[0]->cat_ID; // current category Id $args = array('category'=>$current_cat_id,'orderby'=>'post_date','order'=> 'DESC'); $posts = get_posts($args); // get ids of posts retrieved from get_posts $ids = array(); foreach ($posts as $thepost) { $ids[] = $thepost->ID; } // get and echo previous and next post in the same category $thisindex = array_search($post->ID, $ids); $previd = $ids[$thisindex-1]; $nextid = $ids[$thisindex+1]; if (!empty($previd)){ ?> <a rel="prev" href="<?php echo get_permalink($previd) ?>">Previous</a> <?php } if (!empty($nextid)){ ?> <a rel="next" href="<?php echo get_permalink($nextid) ?>">Next</a> <?php } ?>
I use theme twenty twelve and I have put this code in single.php. Apparently this code do the job, but it only rotate 5 posts not all post in category.
Any advice will be welcome. Thanks
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘I need display posts on same category in links next and previous posts.’ is closed to new replies.