Fixing WordPress Excerpts Word Count in Chinese

Fixing WordPress Excerpts Word Count in Chinese

Some users reported that the WordPress excerpts word count is incorrect in Chinese. This PHP snippet should help:

function dez_filter_chinese_excerpt( $output ) {
global $post;
//check if its chinese character input
$chinese_output = preg_match_all("/p{Han}+/u", $post->post_content, $matches);
if($chinese_output) {
$output = sprintf( '%1$s

%3$s

',
mb_substr( $output, 0, 50 ) . '...',
get_permalink(),
__( 'Read more', 'generatepress' )
);
}
return $output;
}
add_filter( 'get_the_excerpt', 'dez_filter_chinese_excerpt' );

發表回覆

您的郵箱地址不會被公開。 必填項已用 * 標註