Are you looking for a way to automatically link all post thumbnails to the post permalink? While there’s probably a plugin for this, we have created a quick code snippet that you can use to automatically link all post thumbnails to the post permalink in WordPress.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
add_filter( 'post_thumbnail_html', 'wps_post_thumbnail', 10, 3 ); function wps_post_thumbnail( $html, $post_id, $post_image_id ) { $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html . '</a>'; return $html; }
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 43 simple WordPress themes to start a website and how to set up Shopify with WordPress.
Thanks a lot ! 🙂
no problem!
Thank you..
How do I get this to link to the media file instead of the permalink?
Hi there, thanks for sharing…
How it is possible to echo the categories from the thumbnails ID.
I ‘ve tried ” get_the_category( $thumbnail->ID ) “, nothing happens.
Thanks in advance…
Hi… it doesn’t work for me please help.. i want all the images in my all posts get attached to post permalink on home page..
Do you have post thumbnails enabled in your theme? this will not just take any images and link to the post.
[…] Tip: Linking Thumbnails To The Post Permalink Auto link post thumbnails to the post permalink [WordPress SEO] How to Use Post Permalink and Post Title […]
so great but simple ! i am finding it ! …..
Cool glad you like it.
[…] WP-Snipp Twitter deze pagina! […]
Neet! Going to try this ASAP, and post it on my site. I’ll backlink.
Cool thanks a link back is always nice.