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













