Show Previous/Next Posts navigation only inside the same category

Is it possible to force previous/next navigation to browse through the posts (back and forth) that are only inside the same category (and to exclude other categories)?

Related posts

Leave a Reply

2 comments

  1. The functions accept an $in_same_cat argument, the default value of which is false. Just set that to true.

    previous_post_link($format, $link, true);
    next_post_link($format, $link, true);
    
  2. You can use the following code in single.php loop to display nex/pre links in same category

    <?php
    
    
    get_template_part( 'content', get_post_format() );
    
     // Previous/next post navigation.
    previous_post_link('%link', 'Before', true );
    next_post_link( '%link', 'Next', true );
    
    ?>