WordPress调取文章第一张图片地址 支持外链


20160114092456

效果图

若想实现外链特色图像的功能是可以使用插件:Featured Image Via URL,不过奇怪的是WordPress官方据说对它进行封杀,并且实际上只是将外链图片转存到本地服务器上而已。WP酷只能将以前积累的代码拿出来分享给大家,实现效果为:替换原本特色图像功能,设置外链缩略图。

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;
}

调用方式:


蛋痛,但不心痛...