Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at Unwanted.cloud

RSS Latest posts from my personal blog


Subscribe to RSS feed


Add wrapper or change HTML output for post thumbnail in WordPress

Stanislav KhromovStanislav Khromov

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 )
{
    ob_start();
    ?>
    <span class="featured-image">
        <a href="<?=get_the_permalink($post_id)?>">
            <?=$html?>
        </a>
    </span>
    <?php
    return ob_get_clean();
}, 10, 5);
PHP

Full-stack impostor syndrome sufferer & Software Engineer at Schibsted Media Group

Comments 0
There are currently no comments.