WordPress Custom Post Type Navigation

Code snippet for creating navigation links between posts belonging to a specific WordPress Custom Post Type.

I wanted to add links below my videos that would allow users to navigate easily to the Previous or Next video. WordPress doesn’t seem to have this kind of navigation by default on CPTs.

So, here’s how I added it (inside a Catalyst Custom Hook Box) to a custom post type ‘lesson’.

This one will display the name of the previous or next post.

<?php if( is_singular('lesson') ) { ?>
<div><?php previous_post_link( '« %link' ) ?></div>
<div><?php next_post_link( '%link »' ) ?></div>
</div><?php } ?>

This one will display the words Previous and Next.

<?php if( is_singular('lesson') ) { ?>
<div><?php previous_post_link( '%link', 'Previous' ) ?></div>
<div><?php next_post_link( '%link', 'Next' ) ?></div>
</div><?php } ?>
<?php previous_post_link('%link', 'Previous', TRUE); ?>

Previous Next in Same Taxonomy as Current CPT

For some reason this simple solution was hard for me to find. But this does the trick: had a lesson CPT inside a Module Tax. Wanted users to be able to click to the previous and next lessons … but only in the current module.

previous_post_link('<div>%link</div>', 'prev button', TRUE, ' ', 'module' );
next_post_link('<div>%link</div>', 'next button',TRUE, ' ', 'module' );

Source Update as of WP3.8: http://codex.wordpress.org/Function_Reference/previous_post_link
Source: http://codex.wordpress.org/Next_and_Previous_Links

Hire WordPress Experts at Codeable.

The only WordPress exclusive freelancer platform that connects thousands of customers with the top WordPress freelance experts to work on projects of any size. 

Free estimate • No obligation to hire • 100% risk-free