Previous Next Navigation for Custom Post Types

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.

1
2
3
4
5
<?php if( is_singular('lesson') ) { ?>
<div class="post-nav">
<div class="alignleft prev-next-post-nav"><?php previous_post_link( '&laquo; %link' ) ?></div>
<div class="alignright prev-next-post-nav"><?php next_post_link( '%link &raquo;' ) ?></div>
</div><?php } ?>

This one will display the words Previous and Next.

1
2
3
4
5
<?php if( is_singular('lesson') ) { ?>
<div class="post-nav">
<div class="alignleft prev-next-post-nav"><?php previous_post_link( '%link', 'Previous' ) ?></div>
<div class="alignright prev-next-post-nav"><?php next_post_link( '%link', 'Next' ) ?></div>
</div><?php } ?>
1
<?php previous_post_link('%link', 'Previous', TRUE); ?>

Special: 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.

1
previous_post_link('<div class="previous-post-link">%link</div>', 'prev button', TRUE, ' ', 'module' );
1
next_post_link('<div class="next-post-link">%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
Source: http://catalysttheme.com/forum/showthread.php?1941-Post-Nav/