Home / Attachments / Automatically Link Featured Images to Posts
Duplicate Snippet

Embed Snippet on Your Site

Automatically Link Featured Images to Posts

Wrap featured images in your theme in links to posts.

1.9k
Code Preview
php
<?php
/**
 * Wrap the thumbnail in a link to the post.
 * Only use this if your theme doesn't already wrap thumbnails in a link.
 *
 * @param string $html The thumbnail HTML to wrap in an anchor.
 * @param int    $post_id The post ID.
 * @param int    $post_image_id The image id.
 *
 * @return string
 */
function wpcode_snippet_autolink_featured_images( $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', 'wpcode_snippet_autolink_featured_images', 20, 3 );

Comments

Add a Comment