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
<?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
<?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() and wp_get_attachment_image_src() get the neighboring post’s featured image; index 0 is its URL.
  • single-post-thumbnail is a custom image size name. Register it with add_image_size() in your theme, or use a built-in size such as thumbnail or medium.
  • get_permalink() and get_the_title() build the link to the neighboring post.
  • esc_html_e() prints the translatable labels; replace the awa47-nmh47 text 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.

Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More