使用方法
首先将下面代码添加到functions.php文件中
// WordPress获取文章内图片数量
if( !function_exists('get_post_images_number') ){
function get_post_images_number(){
global $post;
$content = $post->post_content;
preg_match_all('/<img.*?(?: |\t|\r|\n)?src=['"]?(.+?)['"]?(?:(?: |\t|\r|\n)+.*?)?>/sim', $content, $result, PREG_PATTERN_ORDER);
return count($result[1]);
}
}
然后在需要统计文章内图片数量的地方添加下面代码即可
<?php echo get_post_images_number().'张图片' ?>