Auto Link Thumbnails To Posts In WordPress

Post thumbnails are usually displayed big in archive pages and people tend to click on them to navigate to posts rather than clicking on post titles. So make post thumbnails link to the posts can help your users have better experience, thus improve website friendliness.

This simple code above will help you do that, put it in the functions.php file of your theme, or better, in a file that optimized for loading in front-end only.

add_filter( 'post_thumbnail_html', 'rw_post_thumbnail_html', 10, 2 );
function rw_post_thumbnail_html( $html, $post_id )
{
    $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
    return $html;
}

2 comments

  1. Thanks for your sharing. If I want to customize photo size, if need to set the picture ‘width’ ‘height’ attribute….

Comments are closed.