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

    automatically-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,