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.
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>
<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'); ?>
</div>