Site logo

[Support request] How to display featured images below the first paragraph of posts and pages

Home Forums Support [Support request] How to display featured images below the first paragraph of posts and pages

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

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2137125
    Roger

    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!

    #2137207
    Ying
    Staff
    Customer Support
    #2137803
    Roger

    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

    #2137808
    David
    Staff
    Customer Support

    Hi there,

    you’re missing a closing } at the very end of that code.

    I added some extra lines of space in the original code block to make it easier to copy.
    See here:

    https://generatepress.com/forums/topic/decrease-the-spacing-between-the-headline-and-image/#post-1672071

    #2137826
    Roger

    Ha! 😀 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

    #2137838
    David
    Staff
    Customer Support

    Awesome – so glad to hear that – and thanks for the kind words 🙂

    #2547740
    pedro paulo

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

    #2547957
    Ying
    Staff
    Customer Support

    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?

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