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!

RSS Latest posts from my personal blog


Most viewed posts


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

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
LinkedIn
Twitter
WordPress.org Profile
Visit my other blog

Comments 0
There are currently no comments.