Auto Link Featured Post Thumbnails to The Post Permalink

How to Auto Link Featured Post Thumbnails to The Post Permalink

in Media
Google+ LinkedIn

Without any doubts, featured post thumbnails are one of the best features of WordPress. That’s why we are always trying to customize them and use them in unique ways on WordPress websites. For example, in previous articles we posted about how to add featured post thumbnails into blog RSS feeds, create an intuitive image based post navigation and automatically pull first image from post content to use as featured image for an article. And now in this article we will show you how to link all post thumbnails to the post permalink without you having to change each post individually.

To do that we will modify the HTML output of featured post thumbnails with the WordPress filter post_thumbnail_html. We will add post permalink into the HTML output, so all post thumbnails will automatically link to the posts. We have created a small function for you to use.

Adding this WordPress snippet to the functions.php of your WordPress theme will automatically link all post thumbnails to the post permalink. It’s as easy as that.

// auto link featured post thumbnails to the post permalink
function wcs_auto_link_post_thumbnails( $html, $post_id, $post_image_id ) {
    $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
    return $html;
}
add_filter( 'post_thumbnail_html', 'wcs_auto_link_post_thumbnails', 10, 3 );

Share the love

If you like this snippet, share it with others!

2 Replies on "Auto Link Featured Post Thumbnails to The Post Permalink"

  1. Harry Jellicoe

    Hey, the snippet is great and in principle it is working. The thumbnails are linking to the post permalink.

    However the code is messing with the image positioning, moving them all out of their frame, further down into a random position in the page. Any ideas of a fix for this?

    Thanks,

    Harry

    1. Hi Harry,

      That is a CSS issue and related to you theme only. You should check it in Firebug and apply the appropriate CSS code.

Leave a Reply

Your email address will not be published. Required fields are marked *