You want has_post_thumbnail
.
add_filter( 'the_content', insert_featured_image, 20 );
function insert_featured_image( $content ) {
global $post;
if (has_post_thumbnail($post->ID)) {
$content = preg_replace( "/<\/p>/", "</p>" . get_the_post_thumbnail($post->ID, 'post-single'), $content, 1 );
}
return $content;
}
Note: The code above is from the post referenced in the OP, but with the has_post_thumbnail
conditional. I also had to add global $post;
to avoid the “undefined variable” Notices
s. Alternately, you can leave that $post
references out as has_post_thumbnail
and get_the_post_thumbnail
will assume the global $post
(which is why the original code works at all).
add_filter( 'the_content', insert_featured_image, 20 );
function insert_featured_image( $content ) {
if (has_post_thumbnail()) {
$content = preg_replace( "/<\/p>/", "</p>" . get_the_post_thumbnail(null, 'post-single'), $content, 1 );
}
return $content;
}