Home › Forums › Support › How to display featured images below the first paragraph of posts and pages
- This topic has 7 replies, 4 voices, and was last updated 2 years, 6 months ago by
Ying.
-
AuthorPosts
-
February 28, 2022 at 2:40 pm #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!
February 28, 2022 at 5:12 pm #2137207Ying
StaffCustomer SupportHi Roger,
Give this solution a try:
https://generatepress.com/forums/topic/decrease-the-spacing-between-the-headline-and-image/#post-1672071March 1, 2022 at 5:30 am #2137803Roger
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,
RogerMarch 1, 2022 at 5:34 am #2137808David
StaffCustomer SupportHi 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:March 1, 2022 at 5:49 am #2137826Roger
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,
RogerMarch 1, 2022 at 6:10 am #2137838David
StaffCustomer SupportAwesome – so glad to hear that – and thanks for the kind words 🙂
February 26, 2023 at 12:05 pm #2547740pedro 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;
}February 26, 2023 at 6:17 pm #2547957Ying
StaffCustomer SupportHi Pedro,
If you add
!important
after the1.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?
-
AuthorPosts
- You must be logged in to reply to this topic.