add_filter( 'the_content', 'put_thumbnail_in_posting' );
function put_thumbnail_in_posting( $content ) {
if ( is_singular('post') && is_main_query() && has_post_thumbnail()) {
$thumbnail = get_the_post_thumbnail( null, '',
array(
'style' => implode( ' ',
array(
'padding: 6px;',
'margin-bottom: 8px;',
'margin-right: 8px;',
'max-width: 100%;',
'border: 1px solid rgb(204, 204, 204);',
'background: none repeat scroll 0% 0% rgb(238, 238, 238);',
)
)
)
);
$content = $thumbnail . $content;
}
return $content;
}
This code includes the is_singular('post') conditional tag which controls the display of the featured image in single posts only.
the_content hook filters the_content, gets the post thumbnail if one has been added to the post using the featured image module and displays it then adds the content after the image.
You could code this differently and also add a fallback featured image in your child themes images folder which displays if one isn't added manually.
add_filter( 'the_content', 'put_thumbnail_in_posting' );
function put_thumbnail_in_posting( $content ) {
if ( is_singular('post') && is_main_query() && has_post_thumbnail()) {
$thumbnail = get_the_post_thumbnail( null, '',
array(
'style' => implode( ' ',
array(
'padding: 6px;',
'margin-bottom: 8px;',
'margin-right: 8px;',
'max-width: 100%;',
'border: 1px solid rgb(204, 204, 204);',
'background: none repeat scroll 0% 0% rgb(238, 238, 238);',
)
)
)
);
$content = $thumbnail . $content;
}
else echo'<img src="' . get_stylesheet_directory_uri() . '/images/fallback.jpg' . '" alt="" />';
return $content;
}
The 2nd code block includes a fallback featured image.
Here's another way to code it:
add_filter( 'the_content', 'featured_image_before_content' );
function featured_image_before_content( $content ) {
if ( is_singular('post') && is_main_query() && has_post_thumbnail()) {
$thumbnail = get_the_post_thumbnail();
$content = $thumbnail . $content;
}
return $content;
}