Bootstrap Templates Bundle

Thursday, February 14, 2013

Setting post image automatically as featured image for post thumbnail

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;
}

3 comments:

  1. very important code for theme developers

    ReplyDelete
  2. Source 1 ::http://wordpress.stackexchange.com/questions/50478/scan-for-first-image-in-post-and-display-it

    Source 2::
    http://wordpress.stackexchange.com/questions/60245/get-first-image-in-a-post

    ReplyDelete
  3. May be this code will not work now so follow
    http://hasibtutorial.blogspot.com/2013/02/php-code-for-getting-thumbnail-and.html

    ReplyDelete

Nightery - Night Club WordPress Theme

Nightery - Night Club WordPress Theme Nightery – Night Club WordPress Theme is an awesome design idea for your Night Club, Conce...