- This topic has 11 replies, 6 voices, and was last updated 3 years, 7 months ago by
Ying.
-
AuthorPosts
-
July 14, 2021 at 8:05 pm #1856179
SHIVAM
Hi team,
I want to show featured image after second Paragraph in single post. Not any option available in theme.July 14, 2021 at 9:28 pm #1856213Elvin
StaffCustomer SupportHi Shivam,
This is quite tricky to do as it requires us to manipulate the actual content to append the featured image on the 2nd line.
You can try this PHP snippet:
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( $feat_img, 2, $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 ); }You then uncheck the “Display featured image” on the customizer settings. ( Appearance > Customize > Layout > Blog ) – This has to be done, else there will be 2 featured image displaying on your page.
January 12, 2022 at 8:45 am #2078174Ali
Hello there Elvin, Thanks for providing the code.
Is there any way to modify this code to display the featured image on ‘pages’ as well and also set the height of the image to max 250px
January 12, 2022 at 9:05 am #2078202David
StaffCustomer SupportHi there,
you would add some CSS like this:
.incontent-featured-image img { max-height: 250px; object-fit: cover; }But it will crop the image if the aspect ratio is different.
January 12, 2022 at 12:12 pm #2078385Ali
the CSS above isn’t working. 🙁
Also, the PHP snippet by Elvin is working for posts but not for pages.
January 12, 2022 at 1:52 pm #2078467Ying
StaffCustomer SupportHi Ali,
Try this PHP snippet to include pages:
add_filter( 'the_content', 'insert_featured_image', 20 ); function insert_featured_image( $content ) { $feat_img = get_the_post_thumbnail($post->ID); if ( (is_single() || is_page()) && ! is_admin() ) { return prefix_insert_after_paragraph( $feat_img, 2, $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 ); }Try this CSS:
.entry-content p:nth-child(2) ~ img { max-height: 250px; object-fit: contain; }You can try
containorcoverfor object-fit to see which one you like better.January 12, 2022 at 3:32 pm #2078540Ali
Thanks 🙂
January 12, 2022 at 3:46 pm #2078546Ying
StaffCustomer SupportNo problem 🙂
October 4, 2022 at 5:22 am #2362587Reza
Hi,
The snippet is affecting the WooCommerce product page as well.
Is there a way to avoid this or use elements to implement a script on specific post categories?
Thanks
October 4, 2022 at 10:56 am #2363126Ying
StaffCustomer SupportHi Reza,
Try change this part
if ( (is_single() || is_page()) && ! is_admin() ) {to
if ( (is_single() || is_page()) && ! is_admin() && ! is_woocommerce() ) {October 4, 2022 at 11:20 am #2363148Reza
Thanks 🙂
October 4, 2022 at 12:39 pm #2363217Ying
StaffCustomer SupportNo problem 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.