Site logo

Archived topic

Featured Image Location Change

3 replies · Started by Dilip on October 7, 2022

Viewing posts 1–4 of 4

Hi,
By default, the featured image is appearing after the headline which might affect the LCP score. I want to show my featured image after few words or before H2.
What should I do?
I am not a technical guy to understand coding. However, I know that using additional CSS, this might be done.
Looking for the solution.

For example the site: http://www.owntheyard.com

Thank you!
Dilip

Hi there,

it can't be done with CSS.
But you can try this PHP Snippet:

add_filter( 'the_content', 'insert_featured_image', 20 );
function insert_featured_image( $content ) {
    global $post;
    $feat_img = get_the_post_thumbnail($post->ID, 'post-single');
    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( '<div class="incontent-featured-image">' . $feat_img . '</div>', 1, $content );
    }
    return $content;
}
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    $closing_p = '<h2>';
    $paragraphs = explode( $closing_p, $content );
    if ($paragraphs) {
        $paragraphs[0] = $paragraphs[0].'<h2>'.$insertion;
    }    
    return implode( '<h2>', $paragraphs );
}

It will insert the featured image before the first H2.

Adding PHP: https://docs.generatepress.com/article/adding-php/

Thank you David for your help.

You're welcome

This archived topic is closed to new replies.