Site logo

[Support request] How to display featured image after two paragraph in Page (Not in Post)

Home Forums Support [Support request] How to display featured image after two paragraph in Page (Not in Post)

Home Forums Support How to display featured image after two paragraph in Page (Not in Post)

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1935340
    Sahidul

    Hi,
    I have got the code for showing the featured image after two paragraphs in a single post but I wanted to do the same thing for the single page. How can I do that? Would you please share the code if possible?
    Thanks in advance.

    #1936100
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    The code should be almost identical. Can you share what you have working for single posts? We may be able to point you in the right direction when it comes to modifications.

    #1936383
    Sahidul

    Hi,
    I am using this code for showing the featured image after two paragraphs in the post. To do this I am using the code snippet plugin. I just want to do the same thing for the page.

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

    #1936400
    David
    Staff
    Customer Support

    Hi there,

    in the code you have look for:

    is_single()

    and change that to:

    is_singular()

    #1936577
    Sahidul

    Hi,
    It is now working. But I found another problem. As I said, I wanted to show the featured image for the post and page after two paragraphs and I created two different filter functions, Now in the single post, the featured image is showing two times, one after one.

    Here is the code I am using.

    For POST:
    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 );
    }

    For PAGE:
    add_filter( ‘the_content’, ‘insert_featured_image_page’, 20 );
    function insert_featured_image_page( $content ) {
    $feat_img = get_the_post_thumbnail($post->ID, ‘post-single’);
    if ( is_singular() && ! is_admin() ) {
    return prefix_insert_after_paragraph_page( $feat_img, 2, $content );
    }
    return $content;
    }
    // Parent Function that makes the magic happen
    function prefix_insert_after_paragraph_page( $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 );
    }

    #1936707
    David
    Staff
    Customer Support

    You don’t need the two codes, the second code using is_singular is all you need.

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