Before using this code I use different types of plugins for showing post thumbnail. But this below code helps me to set my post image automatically as my thumbnail/ featured image, not only that , this code also set a default thumbnail
paste this code into functions.php file
// Get URL of first image in a post
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
// no image found display default image instead
if(empty($first_img)){
$first_img = "http://newsprotidin.com/wp-content/uploads/2013/02/thumb-150x150.jpg";
}
$first_img = vt_resize( $first_img, '', 460, 250, true );
return $first_img;
}
paste this code into functions.php file
// Get URL of first image in a post
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
// no image found display default image instead
if(empty($first_img)){
$first_img = "http://newsprotidin.com/wp-content/uploads/2013/02/thumb-150x150.jpg";
}
$first_img = vt_resize( $first_img, '', 460, 250, true );
return $first_img;
}
very important code for theme developers
ReplyDeleteSource 1 ::http://wordpress.stackexchange.com/questions/50478/scan-for-first-image-in-post-and-display-it
ReplyDeleteSource 2::
http://wordpress.stackexchange.com/questions/60245/get-first-image-in-a-post
May be this code will not work now so follow
ReplyDeletehttp://hasibtutorial.blogspot.com/2013/02/php-code-for-getting-thumbnail-and.html