Site logo

Archived topic

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

5 replies · Started by Sahidul on September 19, 2021

Viewing posts 1–6 of 6

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.

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.

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

Hi there,

in the code you have look for:

is_single()

and change that to:

is_singular()

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

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

This archived topic is closed to new replies.