Previous and Next Post Links Below Posts
Adds links to the previous and next post at the end of each blog post, for themes that do not show post navigation.
Opens WPIDE on
Code
// Only link to posts that share a category with the current post.
$wpide_same_category = false;
add_filter( 'the_content', function ( $content ) use ( $wpide_same_category ) {
if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
$previous = get_previous_post_link( '<div class="wpide-nav-previous" style="flex:1;">← %link</div>', '%title', $wpide_same_category );
$next = get_next_post_link( '<div class="wpide-nav-next" style="flex:1;text-align:right;">%link →</div>', '%title', $wpide_same_category );
if ( ! $previous && ! $next ) {
return $content;
}
return $content . '<nav class="wpide-post-nav" aria-label="Posts" style="display:flex;gap:1.5em;margin-top:2.5em;">' . $previous . $next . '</nav>';
}, 40 );
Notes
Set $wpide_same_category to true to only link to posts in the same category.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.