Show Previous & Next Post In Single Post

Quick Summary
- get_previous_post and get_next_post fetch the neighboring posts for single.php, showing each one's featured image and title link or a fallback message.
- single-post-thumbnail is a custom image size, so register it with add_image_size or switch to a built-in size such as medium.
- Both functions ignore categories by default, so pass true as the first argument to stay within the current post's category.
Previous and next links at the end of a post keep readers moving through your blog. These two snippets show the title and featured image of the neighboring posts, with a fallback message on the first and last post. Add them to your WordPress single post template, such as single.php, inside the loop.
Previous post
get_previous_post() returns the post published just before the current one. If there isn’t one, it returns an empty value and the snippet prints No Previous Post instead.
<?php esc_html_e( 'Previous post', $domain = 'awa47-nmh47' ); ?>
<?php
$prevPost = get_previous_post();
?>
<?php if (empty( $prevPost )): ?>
<p>No Previous Post</p>
<?php endif; ?>
<?php if (!empty( $prevPost )): ?>
<?php $p_t_image = wp_get_attachment_image_src( get_post_thumbnail_id( $prevPost->ID ), 'single-post-thumbnail' );
?>
<img class="post-nav-img" src="<?php echo $p_t_image[0]; ?>" alt="">
<a href="<?php echo get_permalink( $prevPost->ID ); ?>" class="content"><?php echo get_the_title($prevPost) ?></a>
<?php endif; ?>Next post
The second snippet works the same way, but uses get_next_post() to get the post published right after the current one.
<?php esc_html_e( 'Next post', $domain = 'awa47-nmh47' ); ?>
<?php
$nextPost = get_next_post();
?>
<?php if (empty( $nextPost )): ?>
<p>No Next Post</p>
<?php endif; ?>
<?php if (!empty( $nextPost )): ?>
<?php $n_t_image = wp_get_attachment_image_src( get_post_thumbnail_id( $nextPost->ID ), 'single-post-thumbnail' );
?>
<img class="post-nav-img" src="<?php echo $n_t_image[0]; ?>" alt="">
<a href="<?php echo get_permalink( $nextPost->ID ); ?>" class="content"><?php echo get_the_title($nextPost) ?></a>
<?php endif; ?>How it works
get_post_thumbnail_id()andwp_get_attachment_image_src()get the neighboring post’s featured image; index0is its URL.single-post-thumbnailis a custom image size name. Register it withadd_image_size()in your theme, or use a built-in size such asthumbnailormedium.get_permalink()andget_the_title()build the link to the neighboring post.esc_html_e()prints the translatable labels; replace theawa47-nmh47text domain with your theme’s.
By default both functions move between posts of the same post type, whatever their category. Pass true as the first argument to stay in the current category. If a neighboring post has no featured image, its img tag is printed without a source, so add a check if some posts lack one.
Related articles
- related posts by tag without a plugin: another way to keep readers on the blog after a post
- printing a post’s tags and categories: more custom markup for the same single.php template

