1. 当前位置:
  2. 首页
  3. »
  4. 随笔
  5. »
  6. WordPress获取文章全部图片并计算图片数量

WordPress获取文章全部图片并计算图片数量

零分 770

WordPress获取文章所有图片

//获取文章所有图片
function post_imgs(){
global $post, $posts;
$post_img = '';
ob_start();
ob_end_clean();
$pattern="/<img.*?src=[\'|\"](.*?)[\'|\"].*?[\/]?>/";
preg_match_all($pattern, $post->post_content, $matches);
if(!empty($matches[1])){
$imgs = str_ireplace('//','//',$matches[1]);
return $imgs;
}
return false;
}

返回图片是一个数组,调用方式

foreach(post_imgs() as $img){
echo '<img src="'.$img.'">';
}

计算图片数量

count(post_imgs());
头像
支持作者
联系微信二维码
0%