欢迎您光临爱永设计官网!
电话图标 服务热线/微信:13436537174 QQ咨询:276583799

新闻资讯

news

为WordPress添加文章字数统计

发表日期:2012-08-14 文章作者:爱永设计  浏览次数:13853 次

WordPress在后台编辑日志时编辑框左下角有一个字数统计,不过只显示在后台,能不能在前台也加上文章字数统计功能呢?研究了一下程序源文 件,发现中文版WP后台的字数统计功能,是通过wp-content\languages目录的zh_CN-word-count.js实现的,就是不知 道如何调用。网上搜了一下,找到两篇老外给出的代码:

一、把下面代码加到主题的functions.php文件中:

function count_words($str){
$words = 0;
$str = eregi_replace(" +", " ", $str);
$array = explode(" ", $str);
for($i=0;$i < count($array);$i++)
{
if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $array[$i]))
$words++;
}
return $words;
}

然后在single.php中希望显示字数统计的位置加上:

Word count: <?php echo count_words($post->post_content); ?>

二、还是将下面代码加到functions.php文件中,此方法与上面不同的是,还加上了一个估算的阅读时间:

// Custom functions
// START : Show word count
function show_post_word_count(){
ob_start();
the_content();
$content = ob_get_clean();
return sizeof(explode(" ", $content));
}
// END : Show word count
// START : Estimated reading time
if (!function_exists('est_read_time')):
function est_read_time( $return = false) {
$wordcount = round(str_word_count(get_the_content()), -2);
$minutes_fast = ceil($wordcount / 250);
$minutes_slow = ceil($wordcount / 150);
if ($wordcount <= 150) {
$output = __("< 1 minute");
} else {
$output = sprintf(__("%s - %s minutes"), $minutes_fast, $minutes_slow);
}
echo $output;
}
endif;
if (!function_exists('est_the_content')):
function est_the_content( $orig ) {
// Prepend the reading time to the post content
return est_read_time(true) . "\n\n" . $orig;
}
endif;
// END : Estimated reading time

同样在single.php中希望显示字数统计的位置加上:

The following <?php echo show_post_word_count(); ?> words should take about <?php echo est_read_time(); ?> to read.

可惜上述两种方法对汉字统计无效,只适合纯英文站点,网上也没发现与中文博客字数统计相关的文章,没办法还是自己折腾一个吧。

将文章分享到..
相关资讯
最新主题模板
随机新闻
最新网站案例
  • 更多 +我们能做什么

    致力于互联网品牌建设与网络营销,专业领域包括网站建设、网站模板、移动互联网营销、wordpress平台开发等,服务范围涵盖基础的域名服务、主机 服务;企业邮箱、云服务器、网络营销等应用服务,为不同类型的客户提供良好的互联网应用定制解决方案,帮助客户在新的全球化互联网环境中保持优势。

  • 更多 +网站模板优势

  • 更多 +关于爱永设计

    爱永设计工作室一直致力于品牌精美的网页设计、网页制作DIV+CSS布局、JS效果、精美网站模板、标志设计、专业仿站, 低廉的价格,真诚的服务,我们拥有全国各地的客户群体和各行业的成功案例。以一流的服务,出色的网页设计和制作能力,认真严谨的工作态度为客户提供优质满意的服务。期待与您的合作!

Copyright © 2012 - 2024 aysheji.com All Rights Reserved 爱永设计 版权所有
邮箱:aysheji@163.com 在线客服:276583799 模板演示地址:www.aymoban.com 备案号:京ICP备13060102号-3
服务内容: 网页设计 网站建设 网站制作 网站模板 婚庆网站模板 摄影网站 手机网站制作 自适应网站制作