在当前使用的主题模板的functions.php文件<?php和?>之前添加以下代码:
[code]
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];
if(empty($first_img)){ //Defines a default image
$first_img = “/images/default.jpg”;
}
return $first_img;
}
在当前主题模板的index.php文件的内容代码前或后添加以下代码:
<img src=”<?php echo catch_that_image(); ?>” alt=”<?php the_title(); ?>” title=”<?php the_title(); ?>” width=”227px”
height=”200px” />[/code]
文章来源:个人随笔 香格里拉seo (www.xgllseo.com)转载请注明,请尊重版权。