Add wrapper or change HTML output for post thumbnail in WordPress

There’s an awesome filter available called post_thumbnail_html if you want to add a quick wrapper or otherwise change the HTML output of your post thumbnails (which are printed with the_post_thumbnail() or get_the_post_thumbnail() in your theme).

Here’s an example where we add a surrounding div wrapper and also link the image to its permalink:

/** Add wrapper for post thumbnail **/
add_filter('post_thumbnail_html', function( $html, $post_id, $post_thumbnail_id, $size, $attr )
    <span class="featured-image">
        <a href="<?=get_the_permalink($post_id)?>">
    return ob_get_clean();
}, 10, 5);

Leave a Reply

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

Markdown is allowed in comments.