Featured image shortcode

Register the shortcode, ideally in a plugin or functions.php if you have to.

add_shortcode('thumbnail', 'thumbnail_in_content');

function thumbnail_in_content($atts) {
    global $post;

    return get_the_post_thumbnail($post->ID);
}

Add the shortcode to you post content.

[thumbnail]

If you want more features, see this post or the pastebin.


ADDING CAPTIONS AND LINKS

add_shortcode('thumbnail', 'thumbnail_with_caption_shortcode');

function thumbnail_with_caption_shortcode($atts) {
    global $post;

    // Image to display

    $thumbnail = get_the_post_thumbnail($post->ID);

    // ID of featured image

    $thumbnail_id = get_post_thumbnail_id();

    // Caption from featured image's WP_Post

    $caption = get_post($thumbnail_id)->post_excerpt;

    // Link to attachment page

    $link = get_permalink($thumbnail_id);

    // Final output

    return '<div class="featured-image">'
    . '<a href="' . $link . '">'
    . $thumbnail
    . '<span class="caption">' . $caption . '</span>'
    . '</a>'
    . '</div>';
}

RESOURCES

Leave a Comment