How to Automatically Link Featured Images to Posts in WordPress

Recently one of our readers asked if it was possible to automatically link featured images to blog posts in WordPress. Most WordPress themes link featured images to posts by default, but some themes may not do that. In this article, we will show you how to automatically link featured images to posts in WordPress.

Why Link Featured Images to Posts in WordPress?

Images are more engaging than text. Using featured images can boost user engagement on your site.

Usually, featured images are large and take more space than text. They are more colorful, hence more noticeable. They are also easier to click on smaller devices like mobile phones and tablets.

However, if your post thumbnails aren’t clickable, then it makes it harder for users to view your post.

Most WordPress themes link featured images to the posts by default. Some themes may not use that approach, which makes it difficult for you to properly utilize featured images.

Having said that, let’s see how you can automatically link featured images to posts in WordPress.

Automatically Link Featured Images to Posts in WordPress

  • This method required you to add code into your WordPress files. See our beginner’s guide on pasting snippets from web into WordPress.

Simply add this code to your theme’s functions.php file or a site-specific plugin.

1
2
3
4
5
function wpb_autolink_featured_images( $html, $post_id, $post_image_id ) {
$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
return $html;
}
add_filter( 'post_thumbnail_html', 'wpb_autolink_featured_images', 10, 3 );

This code simply adds a link around the code generated to display featured images or post thumbnails on your website.

This code will also add a link around featured images on single post pages. If you don’t want to link featured images on single post to the same post, then use this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function wpb_autolink_featured_images( $html, $post_id, $post_image_id ) {
 
If (! is_singular()) {
     
$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
return $html;
 
} else {
 
return $html;
 
}
 
}
add_filter( 'post_thumbnail_html', 'wpb_autolink_featured_images', 10, 3 );

We hope this article helped you learn how to automatically link featured images to posts in WordPress. You may also want to see our list of 14 best featured image plugins and tutorials for WordPress.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!

Pin It on Pinterest

Add address