菜单
189-0733-7671

wordpress统计当前文章图片数量+附文字数量统计

本文提供两个WordPress函数:pic_total()用于统计文章图片数量,通过正则匹配计算img标签;count_words()用于统计文章字数,可过滤HTML标签后计算中文字符数。调用时分别插入对应PHP代码即可显示结果。

曾凤祥
曾凤祥 技术总监
| | 3 分钟
wordpress统计当前文章图片数量+附文字数量统计

统计图片的数量

wordpress统计当前文章图片数量+附文字数量统计

有些图片比较多wordpress网站会比较需要获取文章图片总数,函数放到functions中即可。调用的地方加入<?php echo pic_total(); ?>

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;
}

统计文章字数的数量

//字数统计
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); ?>

曾凤祥
曾凤祥 技术总监

WordPress 独立站开发领域 10+ 年实践经验,长期专注于外贸独立站搭建与 SEO 优化,累计服务企业客户数千家(含制造业、外贸企业、政府等行业)

不想自己折腾 SEO 和技术细节?

写博客是建立行业权威的第一步,但让文章真正带来询盘,需要专业的技术架构和 SEO 策略支撑。
把技术交给我们,你只管写好内容。

微信二维码

扫码添加微信

189-0733-7671

点击拨打

联系我们