How To Automatically Link Featured Images to Posts in WordPress

In some cases, WordPress themes may display featured images without automatically linking them to their respective posts. However, there might be situations where you want to enhance user experience and SEO benefits by linking these featured images to their corresponding posts. In this tutorial, you will learn how to achieve this without relying on plugins. Please note that you should only use this method if your theme does not already wrap featured images with a link.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Open functions.php

Navigate to your theme’s directory and locate the functions.php file. This file contains all the custom functions for your WordPress theme. Open the functions.php file with a text editor. Add the following code snippet at the end of the file:

function 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', 'autolink_featured_images', 20, 3 );
Code language: PHP (php)

Code Explanation:

  • This code defines a function called autolink_featured_images.
  • It takes three parameters: $html (the HTML for the featured image), $post_id (the ID of the post), and $post_image_id (the ID of the featured image).
  • Inside the function, it wraps the featured image $html with an anchor (<a>) tag that links to the post using get_permalink($post_id).
  • The esc_attr function is used to sanitize the title attribute.
  • Finally, the code adds a filter to post_thumbnail_html to apply this function.

Save, Upload and Test the Automatic Featured Image Linking

Save the functions.php file and upload it back to your theme directory if you accessed it via FTP. If you edited it in the WordPress Dashboard, it should already be saved.

After saving,visit your website and navigate to a post with a featured image. You should now see that the featured image is automatically linked to the corresponding post.

That’s it! In this tutorial, you’ve learned how to automatically link featured images to their respective posts in WordPress, following best coding practices. This method enhances the user experience and improves SEO by making it easier for visitors to access related content. Remember, it’s essential to use this approach only when your theme doesn’t automatically wrap featured images with links.

Leave your feedback and help us improve ๐Ÿถ

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee โ˜•๏ธ or donating via PayPal ๐Ÿ’ฐ.

More free knowledge, because why not?

Your thoughts matter, leave a reply ๐Ÿ’ฌ

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