The following code works well, if its integrated inside the loop and if you need an “img” as result.
/* shortcode for post-thumbnail*/
function post_thumbnail( $atts, $content = null ) {
return '<div id="post_thumbnail">' . get_the_post_thumbnail($post_id, 'thumbnail') . '</div>';
}
add_shortcode("post_thumbnail", "post_thumbnail");
/* Shortcode*/
[post_thumbnail]
But I want to create a shortcode, that show me the post thumbnail as a div background (outside the loop) and I need an option to add an ID (for the post ID). Is that possible?
Nice would be something like this:
[post_thumbnail id="12"]
Try this.
Reference read: Shortcode API.
Although, I’d add a check if the thumbnail exists using has_post_thumbnail(), and if it has, output the shortcode.
EDIT
As image background you’d need:
Notice that you need to set the height of an element to have a container width.
You could use wp_get_attachment_image_src() to get the height and width of an image, and then set it in the style as the width and height of the container, like this: