Site logo

Archived topic

Pushing the Featured Image Down Past The Second Paragraph

9 replies · Started by Susan on March 4, 2021

Viewing posts 1–10 of 10

Hello,

I am looking for a way to show the featured images on the blog page, but have the featured image show only after the second paragraph.

I tried adding the following code below to the functions page, and it shows the image after the second paragraph, but the featured image is still showing at the top before the heading. I don't want it showing up before the heading only once after the second paragraph.

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

I've looked everywhere and have been unable to find a solution. Any help would be greatly appreciated.

Thanks

Susan

Hi there,

go to Customizer > Layout > Blog --> Featured Image >> Posts and uncheck: Display featured images
This will remove the one added by the Theme - it won't affect the one added by that code.

Is there a way to still show thumbnails on the blog page without showing the featured image after the heading? If uncheck the featured image, my blog page has no images, which looks weird.

In a perfect world, I can show an image on the blog page and the featured image shows up after the second paragraph.

I'm sure it's not possible but thought I'd ask.

Thanks

Thanks, that worked perfectly!

Have a good day :)

You're welcome - and you too!

This is exactly what I require as I too want to move the featured image to after the second paragraph

However, when pasting in this code snippet (being posted on a public forum I take it you don't mind it being used) I get the following errors:

Parse Error : syntax error, unexpected '/' on line 11
Parse Error : syntax error, expected 'p' (T_STRING), expecting ';'

Any chance you could suggest a way to resolve this?

I am not a programmer and tend to stay away from adding snippets, but this appears to be the only way I can get the featured image to move to where I'd like it to be.

Thanks

Craig

Awesome thank you, this works perfectly.

It would also be useful for me to be able to add my table of contents to after the featured image.

Do you have any pointers on how to do that?

I am using the "Easy table of contents" plugin which has an option to change the position of the table, but it is limited to 'before the first heading', 'after the first heading, 'top', and 'bottom'.

I originally had it in the sidebar as a widget, however, this is not ideal for mobile users and I'd like to have it appearing just after the featured image

Would you mind starting a new topic - so as not to bog down the OP with notifcations :)

This archived topic is closed to new replies.