WordPress调取文章第一张图片做缩略图

WordPress调取文章第一张图片做缩略图,支持外链。
在Functions.php中加入以下代码:

add_theme_support( ‘post-thumbnails’ );

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
$popimg=get_option( ‘mao10_popimg’);
$first_img = “$popimg”;
}
return $first_img;
}

function mmimg($postID) {
$cti = catch_that_image();
$showimg = $cti;
has_post_thumbnail();
if ( has_post_thumbnail() ) {
$thumbnail_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), ‘thumbnail’);
$shareimg = $thumbnail_image_url[0];
} else {
$shareimg = $showimg;
};
return $shareimg;
}

 

把以下代码加入到要显示缩略图的地方:

<img src=”<?php echo mmimg(get_the_ID()); ?>” alt=”” />