How to make thumbnail images click into blog posts

Do you want to make all the thumbnails link to their respective blog posts? If so, use the following code in the functions.php file:

add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
function my_post_image_html( $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;
}

If you only want the post thumbnails to link to the post permalink in a specific loop, use the following code in the query like Jamal mentioned earlier:

if ( has_post_thumbnail() ) : 
    ?><a href="https://wordpress.stackexchange.com/questions/30542/<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><?php 
        the_post_thumbnail(); 
    ?></a><?php 
endif;

The loop is in the index.php page. Take a look at the WordPress Codex regarding The Loop and you can see where pieces of the code should fall depending on your theme.

Leave a Comment