[Resolved] Featured image inside the article

Home Forums Support [Resolved] Featured image inside the article

Home Forums Support Featured image inside the article

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #708084
    purnima banik

    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

    #708308
    David
    Staff
    Customer Support

    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/

    #708323
    purnima banik

    Thank you for the solution.

    #708333
    David
    Staff
    Customer Support

    You’re welcome

    #1652960
    Matthias

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

    #1653370
    David
    Staff
    Customer Support

    Glad to hear that!

    #1766644
    Rachel

    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

    #1766940
    David
    Staff
    Customer Support

    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 🙂

    #1771813
    Rachel

    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

    #1771870
    Elvin
    Staff
    Customer Support

    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.

    #1772353
    Rachel

    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

    #1772372
    David
    Staff
    Customer Support

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

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

    #1772429
    Reza

    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

    #1773496
    Elvin
    Staff
    Customer Support

    Hi Reza,

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

    Screenshots will do. Thank you.

    #1775572
    Reza

    Hi Elvin,

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

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