[短代码]输出文章第一张图地址

有时候需要调用文章第一张图片作为封面图或者缩略图,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;
}

 

版权声明:
作者:月宅
链接:https://blog.moetwo.com/7265.html
来源:月宅酱的博客
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>