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' );

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注