Show the Featured Image Above Post Content
Prints the featured image at the top of single posts, for themes that only use featured images on archive pages.
Opens WPIDE on
Code
// Image size to display.
$wpide_featured_size = 'large';
add_filter( 'the_content', function ( $content ) use ( $wpide_featured_size ) {
if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() || ! has_post_thumbnail() ) {
return $content;
}
$image = get_the_post_thumbnail( null, $wpide_featured_size, array( 'loading' => 'eager', 'fetchpriority' => 'high' ) );
$caption = get_the_post_thumbnail_caption();
$caption = $caption ? '<figcaption>' . wp_kses_post( $caption ) . '</figcaption>' : '';
return '<figure class="wpide-featured-image" style="margin:0 0 2em;">' . $image . $caption . '</figure>' . $content;
}, 5 );
Notes
Change the image size from large to full or any registered size. Do not use it if your theme already shows the image on single posts.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.