Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.