WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom post type name->('portfolio'), with a taxonomy name 'portfolio_category'.

What I want is to add next and previous links on its single page file which is single-portfolio.php. When I click either of the links, it should redirect to the next or previous post in the same post type and in the same taxonomy term.

I used next_post_link() and previous_post_link() but it seems that it only works in single post but not in single custom post type..

Any suggestions will greatly be appreciated.

share|improve this question
I can recommend the following plugin, next-previous-post-link-plus-for-wordpress I'm using it with 3.5.x without any problems. – ialocin Jul 2 '13 at 13:53

The $taxonomy parameter for both next_post_link and previous_post_link was introduced in Wordpress version 3.8.

When the $in_same_term parameter is set to true, you need to set the $taxonomy parameter to the desired taxonomy. By default, it is set to category. Remember, post_format is also a taxonomy


next_post_link( '%link', 'Next post in category', TRUE, ' ', 'post_format' );

Just a note, do not use next_post and previous_post. It has already being depreciated as from Wordpress version 2.0.0. See wp-includes/deprecated.php#L121 and wp-includes/deprecated.php#L158


The single post links will automatically page between posts within the same post type as the current post's post type is used to retrieve the adjacent posts. Check the source code, get_adjacent_post() which is used by the next and previous post links. Pay particular attention to line 1550 ( currently for version 4.1 )

1550    $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms );
share|improve this answer

Add this code to your single-cpt.php file.

<?php previous_post_link('%link', 'Previous in CPT', TRUE) ?>
<?php next_post_link('%link', 'Next in CPT', TRUE) ?>

Where cpt is the name of your custom post type.

share|improve this answer
previous_post and next_post has been depreciated since v2.0.0 which was released in 2005 – Pieter Goosen Aug 10 '14 at 15:39
Updated @goose. Thanks for the review of my code. – Brad Dalton Aug 11 '14 at 3:28

previous_post_link() and next_post_link() work perfectly with custom post types. You need to paste this code in your single-customposttype.php (which in your case is single-portfolio.php):

<div class="previous-post-link">
                <?php previous_post_link('%link', '<< Previous Post', $in_same_term = true, $excluded_terms = '', $taxonomy = 'the-custom-taxonomy-associated-with-your-custom-post-type'); ?>                    

            <div class="next-post-link">
                <?php next_post_link('%link', 'Next Post >>', $in_same_term = true, $excluded_terms = '', $taxonomy = 'the-custom-taxonomy-associated-with-your-custom-post-type'); ?>                    
share|improve this answer

Try this is with thumbnail:

<?php $prevPost = get_previous_post(); if($prevPost) { ?>
    <li class="previous">
    <?php $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(80,80) ); ?>
        <?php previous_post_link('%link', $prevthumbnail . '<strong>Prev</strong> <span>%title</span>'); ?>
<?php } $nextPost = get_next_post(); if($nextPost) { ?>
    <li class="next">
    <?php $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(80,80) ); ?>
        <?php next_post_link('%link', $nextthumbnail . '<strong>Next</strong> <span>%title</span>'); ?>
<?php } ?>
share|improve this answer
Code-only answer is worst. Try to add some direction or description by editing it. :) – Mayeenul Islam May 1 '14 at 9:12
I think you missed the question here. The OP did not ask for links with thumnails. Also, code only answers is not considered as a good answer – Pieter Goosen Aug 10 '14 at 15:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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