Get The first Image from Post in WP.


Some time we need to get the 1st image of the post , So we can display it on our own way.

For this we can call a function which return the 1st image url .
function get_the_first_image() {
global $post, $posts;
$first_img = ”;
ob_start();
ob_end_clean();
$output = preg_match_all(‘//i’, $post->post_content, $matches);
$first_img = $matches [1] [0];
return $first_img;
}
After get the image we need the content without image free so we can use following filter to get the content image free .
add_filter(‘the_content’,’wpi_image_content_filter’,11);

function wpi_image_content_filter($content){
if (is_home() || is_front_page()){
$content = preg_replace(“/]+\>/i”, “”, $content);
}
return $content;
}

,

Leave a Reply