Created
November 6, 2022 15:28
-
-
Save AndreFCAmorim/01b66845978a886f7e528a7457af4eda to your computer and use it in GitHub Desktop.
Automatically Link Featured Images to Posts on WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment