My problem:
I’m trying to create a filter that will add the featured image of a post to the_content, so that I can have the first paragraph of the_content displayed before this image.
What I basicly want to achieve:
<p>First Paragraph of the_content</p>
<img>The Post's Featured Image</img>
<p>The rest of the_content</p>
I someone is able to help me.
Thanks in advance!
You can do this using the ‘the_content’ filter:
Of course, you can add options to the the_post_thumbnail() function to define which size of thumbnail you’d like to use, etc… http://codex.wordpress.org/Function_Reference/the_post_thumbnail
Technically the quickest solution would be to use a shortcode in your content. Otherwise you’ll need a good handle on regex to dump the image between paragraphs using a filter.
Best way would be to add this to functions.php
After your first paragraph in the content just type
[featured_image]
.Note –
Using some kind of regex matching you can do this. Here’s one of them. Just drop in this snippet in yout theme’s
functions.php
file so that it will print content of variable$img
just after first paragraph (i.e. after first occurrence of</p>
tag) in your post content.passing current posts thumbnail / featured image value to $img will print that image after first paragraph.
Modified this to append variable after first paragraph.
To customise the number of paragraphs before the featured image, I adapted the above answer and adapted the code from an ad placement solution.
To use: Add the following two snippets to functions.php and customise the numeral where indicated to control the number of paragraphs.
This Is Simple Process to Done it. Follow Simple Step.