在主题文件functions.php文件中,添加以下函数代码。
//获取文章中的图片个数
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().'张图片' ?>
本文摘自网络,不代表短经典网立场 https://www.duanjingdian.com/238.html