- This topic has 17 replies, 6 voices, and was last updated 2 years, 1 month ago by
Elvin.
-
AuthorPosts
-
October 23, 2018 at 7:02 am #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
PurnimaOctober 23, 2018 at 9:25 am #708308David
StaffCustomer SupportHi 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/
October 23, 2018 at 9:32 am #708323purnima banik
Thank you for the solution.
October 23, 2018 at 9:40 am #708333David
StaffCustomer SupportYou’re welcome
February 10, 2021 at 6:21 am #1652960Matthias
Thank you David,
this was exactly what I was looking for!
Best,
MattFebruary 10, 2021 at 9:19 am #1653370David
StaffCustomer SupportGlad to hear that!
May 5, 2021 at 9:50 pm #1766644Rachel
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,
RachelMay 6, 2021 at 2:14 am #1766940David
StaffCustomer SupportHi 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 🙂May 9, 2021 at 5:06 pm #1771813Rachel
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,
RachelMay 9, 2021 at 7:02 pm #1771870Elvin
StaffCustomer SupportHi 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.May 10, 2021 at 3:37 am #1772353Rachel
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,
RachelMay 10, 2021 at 3:58 am #1772372David
StaffCustomer SupportIts one or the other.
You should just change this line:return prefix_insert_after_paragraph( $feat_img, 2, $content );
May 10, 2021 at 4:46 am #1772429Reza
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
May 10, 2021 at 9:28 pm #1773496Elvin
StaffCustomer SupportHi Reza,
Can you show us the exact code you’ve pasted on the code snippets plugin?
Screenshots will do. Thank you.
May 12, 2021 at 3:20 am #1775572Reza
Hi Elvin,
Here is a screenshot
[img]https://i.imgur.com/y9XmJ0i.png[/img] -
AuthorPosts
- You must be logged in to reply to this topic.