获取文章(相册)中图片的数量
获取文章图片数量函数
将函数post_img_number放在functions.php中。


function post_img_number(){
global $post, $posts;
ob_start();
ob_end_clean();
 
//使用do_shortcode($post->post_content) 是为了处理在相册的情况下统计图片张数
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i',do_shortcode($post->post_content), $matches);
$cnt = count( $matches[1] );
return $cnt;
}

注:本函数最大的改进是通过使用do_shortcode($post->post_content)来调用文章的内容而不是使用$post->post_content,可以解决使用原生相册功能功能时图片不能统计的问题。
调用函数


<?php echo post_img_number().’张’; ?>