当前位置: 首页 » 笔记 » WordPress获取文章全部图片并计算图片数量正文

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

  • 零分
  • 637

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());

声明:博客中如无特殊说明或标注的文章均为博客原创文章,部分文章来源互联网,如有侵犯您的版权,或者对零分博客发布的文章有异议,请来信告知删除、修改,如您有好的建议或者意见也可以来信,谢谢!

请TA喝杯水^_^ 如本文“对您有用”,欢迎随意打赏,让我们坚持创作! 请TA喝水