有时候需要调用文章第一张图片作为封面图或者缩略图,catch_that_image()为输出的函数,可以用echo输出地址

//获取文章中第一张图片的路径并输出
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 = "/images/moeik.png";
}//这里moeik.png为没有文章图时则输出默认的备用图
return $first_img;
}

 

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。