Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to display the feature image inside a post text, perhaps after the third or fourth paragraph (or any) into the_content.

Searching the site I've found this code, by Amit:

add_filter('the_content', function($content)
   $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
   $img = '<img src="'.$url.'" alt="" title=""/>';
   $content = preg_replace('#(<p>.*?</p>)#','$1'.$img, $content, 1);
   return $content;

Works like a charm, but I can't figure out how include the post_thumbnail after a defined paragraph (not only the second). Can anybody point me on the right direction?

Thanks in advance!!

share|improve this question
How would this "defined" paragraph would look like? – Marcin Bobowski Feb 13 '13 at 23:41
Just as any paragraph... I don't know the content. I can 'inject' a class on the <p> tag with jQuery $('p:nth-child(5n+1) span.Noted').addClass('Note1');. Perhaps it helps. – José Luis Quintero Feb 14 '13 at 14:08

1 Answer 1

we can solve this problem by creating a shortcode so that you can insert the featured image where ever you want through the editor itself.Shortcode is a keyword that we created.Any number of custom shortcodes can be created and used either in template or in editor as like wordpress default shortcodes like [gallery].

write a function in functions.php to retrieve featured image .

function myTheme_featured_image($atts, $content){
    $atts = shortcode_atts( array(),$atts, 'featured');
    $post_id = get_the_ID();
     return "<div class='featured_wrapper_div'>".
        get_the_post_thumbnail( $post_id, 'thumbnail' ) 
add_shortcode('featured', 'myTheme_featured_image');

'featured' is our custom shortcode name.In your post/ page editor, give the shortcode name like the following where you want the image, after or before any paragraph / any line / anywhere..


you can style the wrapper div 'featured_wrapper_div' through style.css so that the image fit as you like. Since we are adding this in our editor, it will display when you call the_content() in template

Since we are adding this in editor, it will show if you call the_content() in tempalte.

It will work..

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.