I've been searching for this a lot...but all I get is not what I need. I simply want to show next and previous links on single.php (from all the categories, based on publish date, not just in the same category), but skipping a category. So if the next (or previous) post would be in category id 402, skip the post and get the next (or previous) one.

this simple code <?php next_post_link(); ?> works very well, but I just need it to skip the posts that are in category 402 I also do not want to use the title of the posts, just the links added to "Next" and "Previous" as the link anchor

thank you

2 Answers 2


Use the 4th parameter $excluded_terms http://codex.wordpress.org/Function_Reference/next_post_link

<?php next_post_link( $format, $next, $in_same_term = true, $excluded_terms = '402', $taxonomy = 'category' ); ?>

Same for the previous_post_link http://codex.wordpress.org/Template_Tags/previous_post_link

$previous = '<span class="meta-nav">Previous Post</span>';
$next = '<span class="meta-nav">Next Post</span>';

<?php previous_post_link( $format, $previous, $in_same_term = true, $excluded_terms = '402', $taxonomy = 'category' ); ?>

Or you could create a template tag like whats included in Twenty Fourteen

if ( ! function_exists( 'twentyfourteen_post_nav' ) ) :
 * Display navigation to next/previous post when applicable.
 * @since Twenty Fourteen 1.0
function twentyfourteen_post_nav() {
    // Don't print empty markup if there's nowhere to navigate.
    $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
    $next     = get_adjacent_post( false, '', false );

    if ( ! $next && ! $previous ) {

    <nav class="navigation post-navigation" role="navigation">
        <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'twentyfourteen' ); ?></h1>
        <div class="nav-links">
            if ( is_attachment() ) :
                previous_post_link( '%link', __( '<span class="meta-nav">Published In</span>%title', 'twentyfourteen' ) );
            else :
                previous_post_link( '%link', __( '<span class="meta-nav">Previous Post</span>', 'twentyfourteen' ) );
                next_post_link( '%link', __( '<span class="meta-nav">Next Post</span>', 'twentyfourteen' ) );
        </div><!-- .nav-links -->
    </nav><!-- .navigation -->
  • tried the code with $excluded_terms on single.php, but it shows nothing.
    – vyperlook
    Commented Jul 28, 2014 at 8:41
  • Please paste your code. Some work is required on your behalf to build the code. Commented Jul 28, 2014 at 9:06
  • I just have the usual code... <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>, nothing special
    – vyperlook
    Commented Jul 28, 2014 at 13:02

This is what the Custom Post Types are for - separate unrelated types of content. For your description it sounds like category 402 does not contain content that is relevant to the reader when he navigates the site, but the next/prev links are not the only place you might need to handle and you also will need to hack the calendar widget, recent posts and comments widget, RSS feed generation, and then there will be all kinds of plugins that you will need to hack as well.

You probably can hack those next/prev functions, or even better, write one of your own instead, but the future proof way is to change your code to use CPT instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.