Site logo

Archived topic

Featured image inside the article

17 replies · Started by purnima banik on October 23, 2018

Viewing posts 1–15 of 18

Hi Leo,

I want the featured image after the second paragraph of my every post in the single view. Could you help me in achieving this?

Thanks
Purnima

Hi there,

could give this Snippet a shot:

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

Then use the Layout Element to disable featured images on all posts to remove the default one.

https://docs.generatepress.com/article/layout-element-overview/

Thank you for the solution.

You're welcome

Thank you David,
this was exactly what I was looking for!
Best,
Matt

Glad to hear that!

Hi David,
Is this code still the best way to place the featured image within the content of a post if I have both GP Premium and Generateblocks?

Thanks,
Rachel

Hi Rachel,

that code is the only way
The GP Elements module only works with Action Hooks, which are 'placeholders' in the sites code.
In your Post Content there are no Action Hooks so it requires a Filter Hook :)

Hi David,

Will the featured post image still show on blog and Wp-show-post collections?

If I wanted to change it to show after paragraph 3, how would I do that?

Thanks,
Rachel

Hi Rachel,

It should apply. the_content filter is a WordPress function and its effect is global to all things that use the content of a post.

If I wanted to change it to show after paragraph 3, how would I do that?

David's code has this line:

if ( $paragraph_id == $index + 1 )

Change the number 1 to 2 or 3.

Ok, so just checking the 2 in the top section of the code doesn't need to be changed as well?

return prefix_insert_after_paragraph( $feat_img, 2, $content );

Thanks,
Rachel

Its one or the other.
You should just change this line:

return prefix_insert_after_paragraph( $feat_img, 2, $content );

Hi,

I tried the snippet. It is working but the following notice is showing at the beginning of each blog

Notice: Undefined variable: post in /var/www/my-site.com/wp-content/plugins/code-snippets/php/snippet-ops.php(469) : eval()'d code on line 3

Notice: Trying to get property 'ID' of non-object in /var/www/my-site.com/wp-content/plugins/code-snippets/php/snippet-ops.php(469) : eval()'d code on line 3

Any idea what I might have done wrong?

Cheers

Hi Reza,

Can you show us the exact code you've pasted on the code snippets plugin?

Screenshots will do. Thank you.

Hi Elvin,

Here is a screenshot
[img]https://i.imgur.com/y9XmJ0i.png[/img]

This archived topic is closed to new replies.