Site logo

Archived topic

How to display featured images below the first paragraph of posts and pages

7 replies · Started by Roger on February 28, 2022

Viewing posts 1–8 of 8

Hello,

I am using GeneratePress 3.1.3 and GP Premium 2.1.2
I looked across the support forum, but could not find an answer that worked for my needs.

How do I make it so the featured image for both posts and images displays after (below) the first paragraph of a page or post?

Can anyone provide some help on that?

Thanks!

Hi Ying,

Yes, I saw this previously, it does not work. When This code is used:

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;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    $closing_p = '</p>';
    $paragraphs = explode( $closing_p, $content );
    foreach ($paragraphs as $index => $paragraph) {
        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $closing_p;
        }
        if ( $paragraph_id == $index + 1 ) {
            $paragraphs[$index] .= $insertion;
        }
    }
    return implode( '', $paragraphs );

It returns a parse error:

Parse error: syntax error, unexpected end of file. The line number it references is this:

return implode( '', $paragraphs );

How can we create a working copy of this code (how can the ending be corrected)?
Sorry I'm not a coder, I can edit existing code, but not create new code.

Thanks,
Roger

Ha! :D Done!
Oops... Sorry, I didn't originally see that last } to copy as well.

Thank you!
Perfect!

Much appreciated the follow up. You support has always been great, and a great team all around, all covering each other and us.
I also really like that you all document things, because that also helps us find solutions (I've found tons of useful tweaks here already without having to ask and wait) without having to use a support ticket system.

Thank you,
Roger

Awesome - so glad to hear that - and thanks for the kind words :)

David, I used the PHP above and worked great! But whem I use the CSS bellow to add space after feature image it doesn't work in mobile.

.incontent-featured-image {
margin-bottom: 1.5em;
}

Hi Pedro,

If you add !important after the 1.5em like this, does it work?

.incontent-featured-image {
margin-bottom: 1.5em !important;
}

If it's not, any chance you can link us to your site?

This archived topic is closed to new replies.