正在查看 2 個帖子:1-2 (共 2 個帖子)
-
作者帖子
-
具體效果如下圖:
提供了一個簡碼 [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' );
我感覺太累贅,字太多了,需要修改下,只顯示全部字符統計即可
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 個帖子)
- 哎呀,回覆話題必需登錄。