# 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.

- Type: PHP
- Runs: Frontend only
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: navigation, posts, internal-links
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/previous-next-post-links/

## Code

```php
// 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;">&larr; %link</div>', '%title', $wpide_same_category );
    $next     = get_next_post_link( '<div class="wpide-nav-next" style="flex:1;text-align:right;">%link &rarr;</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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Previous and Next Post Links Below Posts" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
