Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.