wordpress抓取文章第一张图片为缩略图函数
用代码实现wordpress抓取文章第一张图片为缩略图函数,把下面的代码复制到主题文件夹下的functions.php中
//**************************************************************************
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)){
$first_img = bloginfo('template_url'). '/screenshot.png';
}
return $first_img;
}
//*************************************
在要显示缩略图的地方调用如下代码
<img src="<?php echo catch_that_image(); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" width="227px" height="200px" /></a>
正好用上,不过有插件好像可以解决
[回复]作者您好,改代码好象无效哦
[回复]