Site logo

[Resolved] Featured image after Second Paragraph

Home Forums Support [Resolved] Featured image after Second Paragraph

Home Forums Support Featured image after Second Paragraph

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1856179
    SHIVAM

    Hi team,
    I want to show featured image after second Paragraph in single post. Not any option available in theme.

    #1856213
    Elvin
    Staff
    Customer Support

    Hi 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.

    #2078174
    Ali

    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

    #2078202
    David
    Staff
    Customer Support

    Hi 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.

    #2078385
    Ali

    the CSS above isn’t working. 🙁

    Also, the PHP snippet by Elvin is working for posts but not for pages.

    #2078467
    Ying
    Staff
    Customer Support

    Hi 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 containor coverfor object-fit to see which one you like better.

    #2078540
    Ali

    Thanks 🙂

    #2078546
    Ying
    Staff
    Customer Support

    No problem 🙂

    #2362587
    Reza

    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

    #2363126
    Ying
    Staff
    Customer Support

    Hi Reza,

    Try change this part if ( (is_single() || is_page()) && ! is_admin() ) {

    to
    if ( (is_single() || is_page()) && ! is_admin() && ! is_woocommerce() ) {

    #2363148
    Reza

    Thanks 🙂

    #2363217
    Ying
    Staff
    Customer Support

    No problem 🙂

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