[Support request] Featured Image Location Change

Home Forums Support [Support request] Featured Image Location Change

Home Forums Support Featured Image Location Change

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2366746
    Dilip

    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

    #2367016
    David
    Staff
    Customer Support

    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/

    #2367484
    Dilip

    Thank you David for your help.

    #2367633
    David
    Staff
    Customer Support

    You’re welcome

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.