Home / functions.php / Auto link post thumbnails to the post permalink
  • How to Auto link post thumbnails to the post permalink


    Adding this code to the functions.php of your wordpress theme will Automatically link all post thumbnails to the post permalink.

    wordpress snippet : PHP

    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;

    ( WordPress codex functions, hooks, in this snippet. )

    get_permalink, get_post, get_post_field, add_filter, esc_attr, wp,
  • http://www.tekortschot.nl Paul te Kortschot

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

    • http://wpsnipp.com Kevin Chard

      Cool thanks a link back is always nice.

  • Pingback: Thumbnail linken Wordpress | tekortschot.nl()

  • http://muasamvui.com Muasamvui

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

    • http://wpsnipp.com Kevin Chard

      Cool glad you like it.

  • Pingback: How to link Post Thumbnail to the Post Permalink? - Wordpress Arena()

  • http://www.facebook.com/djpanky Pankaj Sharma

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

    • http://wpsnipp.com Kevin Chard

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

  • Dennisnbw

    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…

  • webdiddy

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

  • Ne Farkeder

    Thank you..

  • http://makadesigns.me/ makadesigns.me

    Thanks a lot ! :)

    • http://wpsnipp.com Kevin Chard

      no problem!