X

How to Auto Link Post Thumbnails to the Post Permalink

Snippets by IsItWP

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.

Comments   Leave a Reply

  1. makadesigns.me March 22, 2014 at 1:15 pm

    Thanks a lot ! 🙂

    1. no problem!

  2. Thank you..

  3. How do I get this to link to the media file instead of the permalink?

  4. 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…

  5. 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..

    1. Do you have post thumbnails enabled in your theme? this will not just take any images and link to the post.

  6. How to link Post Thumbnail to the Post Permalink? - Wordpress Arena June 29, 2011 at 9:05 pm

    […] 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 […]

  7. so great but simple ! i am finding it ! …..

    1. Cool glad you like it.

  8. Thumbnail linken Wordpress | tekortschot.nl March 31, 2011 at 2:54 pm

    […] WP-Snipp Twitter deze pagina! […]

  9. Paul te Kortschot March 30, 2011 at 4:28 pm

    Neet! Going to try this ASAP, and post it on my site. I’ll backlink.

    1. Cool thanks a link back is always nice.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!