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):
The
$taxonomy
parameter for bothnext_post_link
andprevious_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 tocategory
. Remember,post_format
is also a taxonomyExample:
Just a note, do not use
next_post
andprevious_post
. It has already being depreciated as from WordPress version 2.0.0. See wp-includes/deprecated.php#L121 and wp-includes/deprecated.php#L158EDIT
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 )Add this code to your single-cpt.php file.
Where cpt is the name of your custom post type.
Try this is with thumbnail: