WordPress 代码段,统计文章的中英文字数总合 然后通过简码调用

WordPress 代码段,统计文章的中英文字数总合 然后通过简码调用


正在查看 2 个帖子:1-2 (共 2 个帖子)
  • 作者
    帖子
  • 诗语
    管理员
    • 文章数量: 5,865
    @feibisi
    楼主

    具体效果如下图:

    提供了一个简码 [character-count],以便您可以在文章的任何位置调用字符统计。将以下代码复制并粘贴到您的 functions.php 文件中或其他的自定义代码段插件里。

    function character_count_shortcode( $atts ) {
        $post_id = get_the_ID();
        $post_content = get_post_field( 'post_content', $post_id );
        $english_count = preg_match_all( '/\p{L}+/u', $post_content, $matches );
        $chinese_count = preg_match_all( '/[\x{4e00}-\x{9fa5}]/u', $post_content, $matches );
        $total_count = $english_count + $chinese_count;
        return '<p>本文共包含 '.$total_count.' 个字符,其中 '.$chinese_count.' 个中文字符,'.$english_count.' 个英文字符。</p>';
    }
    add_shortcode( 'character-count', 'character_count_shortcode' );
    诗语
    管理员
    • 文章数量: 5,865
    @feibisi
    楼主

    我感觉太累赘,字太多了,需要修改下,只显示全部字符统计即可

    function character_count_shortcode( $atts ) {
        $post_id = get_the_ID();
        $post_content = get_post_field( 'post_content', $post_id );
        $english_count = preg_match_all( '/\p{L}+/u', $post_content, $matches );
        $chinese_count = preg_match_all( '/[\x{4e00}-\x{9fa5}]/u', $post_content, $matches );
        $total_count = $english_count + $chinese_count;
        return '<p>本文字数 '.$total_count.'</p>';
    }
    add_shortcode( 'character-count', 'character_count_shortcode' );

    删掉

    ,其中 '.$chinese_count.' 个中文字符,'.$english_count.' 个英文字符。

正在查看 2 个帖子:1-2 (共 2 个帖子)
  • 哎呀,回复话题必需登录。

未找到对应子版块?请直接在 [疑难杂症] 版块中发帖提问。

收藏纪念品


有奖活动

参与论坛社区问答,有机会获赠文派瓦普(Wapuu.com)手办模型。

话题信息

商业系统