众所周知,wordpress特色图是不允许外链图片地址的。不知道wordpress官方为何不允许这样做,确实让人很难理解。那该怎么去做呢?

外链图片设置为特色图


// 特色图支持外链
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('//i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //定义一个默认图像
$default_img=get_template_directory_uri().'/images/thumbnail.png';
$first_img = "$default_img";
}
return $first_img;
}
 
function mimelove_img($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 mimelove_img(get_the_ID()); ?>" alt="" >  //当然,你也可以只用php函数