使用方法
首先将下面代码添加到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().'张图片' ?>
其他方法教程
1.图片统计代码
有些图片比较多wordpress网站会比较需要获取文章图片总数,函数放到functions中即可。
function pic_total() { global $post; $post_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/\<img.+?src="(.+?)".*?\/>/is ', $post->post_content, $matches, PREG_SET_ORDER); $post_img_src = $matches [0][1]; $cnt = count($matches); return $cnt; }
调用代码
<?php echo pic_total(); ?>
2.文字统计代码
//字数统计 function count_words ($text) { global $post; if ( '' == $text ) { $text = $post->post_content; if (mb_strlen($output, 'UTF-8') < mb_strlen($text, 'UTF-8')) $output .= '本文共' . mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8') . '个字'; return $output; } }
调用方式
<?php echo count_words ($text); ?>