- This topic has 5 replies, 3 voices, and was last updated 4 years, 6 months ago by
David.
-
AuthorPosts
-
September 19, 2021 at 1:04 am #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.September 19, 2021 at 7:57 pm #1936100Tom
Lead DeveloperLead DeveloperHi 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.
September 20, 2021 at 3:16 am #1936383Sahidul
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 );
}September 20, 2021 at 3:28 am #1936400David
StaffCustomer SupportHi there,
in the code you have look for:
is_single()and change that to:
is_singular()September 20, 2021 at 6:59 am #1936577Sahidul
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 );
}September 20, 2021 at 7:49 am #1936707David
StaffCustomer SupportYou don’t need the two codes, the second code using
is_singularis all you need. -
AuthorPosts
- You must be logged in to reply to this topic.