Blog   LIVE CHAT

  1. Home
  2. WordPress Tutorials
  3. How To Link Featured Images to Your WordPress Posts Automatically

How To Link Featured Images to Your WordPress Posts Automatically

You need a better, faster web host. Free Site Migration, Amazingly Fast, Secure & Eco-Friendly WordPress Hosting with 24/7 Support. Check Out Our Hosting Plans

Believe it or not, there are still some great WordPress themes out there that don’t link WordPress featured images automatically. These days most themes already perform this function, but for the themes that don’t, there is a fairly simple way that you can get a WordPress featured image to link automatically.

In this article, I will show you how to place some simple code that will solve the problem of linking WordPress featured images to your posts.

There may be any number of reasons you would want to add a WordPress featured image into your posts. Basically, images are more engaging than text.

Readers may see some engaging intro text, but having linking WordPress featured images will make them even more likely to click through to your post.

You don’t have to worry about duplicate featured images either. There is a quick and easy way to fix a duplicate WordPress featured image if you need to.

You will need to know how to add some code into your WordPress site files. It isn’t difficult and you can even read more about it here before you complete the following process.

The first thing you want to do is navigate to your functions.php file inside your WordPress theme. To get there click on Appearance > Editor, and then click on the Theme Functions, (functions.php) file.

Click on appearance and then editor

That function will take you to the following screen, where you will click on the Theme Functions, (functions.php) file.

Click on the functions.php file to edit

You are now in the correct area to add the following code:

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 );

What this code does is adds a link around the code generated to display WordPress featured images or post thumbnails on your website.

It will also add a link around featured images on single post pages. If you do not want to link featured images on single post to the same post, then use the following code instead:

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 );

I hope this article provided you with the information you need to add a custom link to featured images. Again, most WordPress themes do this automatically, but if you happen to be using one that doesn’t, the quick code additions above will link WordPress featured images to your posts automatically.

What other reasons do you have for linking WordPress featured images? Have you found a plugin that does this without the code?

Author: Jeremy Holcombe

Updated on August 19, 2019

Was this article helpful?

Related Articles

Comments

  1. Can you please correct the code without sytax errors.??

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.