Code Snippets

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:

Hire a WordPress Expert

Get hourly and project based theme customization, theme tweaks, and development help from an experienced Kelowna WordPress developer.  We’ve been building WordPress websites full-time for over a decade. We can help you.