/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function vp_get_thumb_url($text) { global $post; $imageurl=""; // extract the thumbnail from attached imaged $allimages =&get_children('post_type=attachment&post_mime_type=image&post_parent=' . $post->ID ); foreach ($allimages as $img){ $img_src = wp_get_attachment_image_src($img->ID); break; } $imageurl=$img_src[0]; // try to get any image if (!$imageurl) { $imageurl=$matches[1]; } // try to get youtube video thumbnail if (!$imageurl) { preg_match("/([a-zA-Z0-9\-\_]+\.|)youtube\.com\/watch(\?v\=|\/v\/)([a-zA-Z0-9\-\_]{11})([^<\s]*)/", $text, $matches2); $youtubeurl = $matches2[0]; if ($youtubeurl) $imageurl = "http://i.ytimg.com/vi/{$matches2[3]}/1.jpg"; else preg_match("/([a-zA-Z0-9\-\_]+\.|)youtube\.com\/(v\/)([a-zA-Z0-9\-\_]{11})([^<\s]*)/", $text, $matches2); $youtubeurl = $matches2[0]; if ($youtubeurl) $imageurl = "http://i.ytimg.com/vi/{$matches2[3]}/1.jpg"; } return $imageurl; } //Use global $post; $thumb=vp_get_thumb_url($post->post_content); if ($thumb!='') echo '<img style=" width:150px;" src="'.$thumb.'" alt="'. get_the_title().'" />
URL: http://www.prelovac.com/vladimir/simple-wordpress-thumbnail-function