要获取WordPress文章内的图片数量,你可以使用以下代码片段:
一、在你的主题文件(例如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().'张图片' ?>