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!!
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!!
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' )
."</div>";
}
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..
[featured][/featured]
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..
$('p:nth-child(5n+1) span.Noted').addClass('Note1');
. Perhaps it helps. – José Luis Quintero Commented Feb 14, 2013 at 14:08