Site logo

Archived topic

Featured Image above first H2 title tag

3 replies · Started by Manu on September 1, 2021

Viewing posts 1–4 of 4

I am trying to place the featured image above the first H2 title tag using the code provided in https://generatepress.com/forums/topic/featured-image-above-first-heading/#post-1903443

add_filter( 'the_content', 'insert_featured_image', 20 );
function insert_featured_image( $content ) {
    $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( '', $paragraphs );
}

The above PHP Snippet provided is placing the featured image after the first H2 tag. But the paragraphs after the remaining H2 tags are converting to H2.
Reference URL: https://selectbestappliances.com/dummy-post/

Also, in the above code, I replaced the </h2> with <h2>, then the featured image shows above the first H2 tag. But the following H2 tags are converting to plain text, without <h2> or <p>.
Reference URL: https://www.techabettor.com/dummy-post/

I would like to place the featured image above the first H2 title tag without affecting the following title tags. Would you please help me to resolve this issue?

Thank You

Hi Manu,

I think the issue is with this line.

return implode( '', $paragraphs );

Your code didn't return the tag.

Try this:

add_filter( 'the_content', 'insert_featured_image' );

function insert_featured_image( $content ) {

    $html = '<div class="incontent-featured-image">' . get_the_post_thumbnail($post->ID, 'post-single') . '</div>';

    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $html, 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] = $insertion.$paragraphs[0];
    }    
    return implode( '<h2>', $paragraphs );
}

Hi Elvin,
Thank you for the suggestion.
The code you now provided is throwing the following error and is not working as expected.

Warning: Undefined variable $post in /public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(469) : eval()'d code on line 5

Warning: Attempt to read property "ID" on null in /public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(469) : eval()'d code on line 5

However, I tweaked the line return implode( '', $paragraphs ); in the first code to get the solution.

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 );
}

Thank You

Nice one. Good catch. Glad you got it sorted. :D

This archived topic is closed to new replies.