WordPress 程式碼段,統計文章的中英文字數總合 然後透過簡碼呼叫

  • 該話題包含 1 個回覆,1 人參與,最後由詩語 更新於 2 年前
正在檢視 2 個帖子:1-2 (共 2 個帖子)
  • 作者
    帖子
  • 詩語
    管理員
    • 文章數量: 5,926
    @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,926
    @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 個帖子)
  • 哎呀,回覆話題必需登入。

話題資訊

  • 當前位於:功能改進
  • 1 條回覆
  • 1 個參與人
  • 最後回覆:<a href="https://bbs.weixiaoduo.com/users/feibisi/" title=" 檢視詩語的個人資料" class="bbp-author-link"><span class="bbp-author-name"> 詩語</span></a>
  • 上次活動:<a href="https://bbs.weixiaoduo.com/topic/41951/#post-41952" title=" 回覆至:WordPress 程式碼段,統計文章的中英文字數總合 然後透過簡碼呼叫">2 年前</a>