Excluding a category from next and previous post links

The next_post_link() and previous_post_link() functions have parameteres as follows –

  • $format (string) – You’ve already included this.

  • $link (string) – You’ve already included this.

  • $in_same_term = false (boolean) – Whether or not all linked posts should be within the same taxonomy term. Chances are this should be false if you are looking to exclude a single term.

  • $excluded_terms="" (string|array) – The terms to exclude, as a comma seperated string or an array of integers.

  • $taxonomy = 'category' (string) – Only required if $in_same_cat = true.

So to get your links working as you desire you should do this –

<?php $excluded_terms="4835"; ?>
<div class="next_prev_cont">
    <div class="left">
        <?php previous_post_link('%link', '<i>Previous post</i><br />%title', false, $excluded_terms); ?> 
    </div>
    <div class="right">
        <?php next_post_link('%link', '<i>Next post</i><br />%title', false, $excluded_terms); ?> 
    </div>
    <div class="clear"></div>

I recommend you take a few moment to read the related function referneces for these two functions –

Leave a Comment