Size: XS


UX & Product Designer - Chi Wai Li

My Blog



Blog

WordPress the_excerpt() not working with Chinese Text



WordPress’ excerpt is pretty useful, if the article doesn’t have predefined text as excerpt, it would  automatically create one using the first 55 words from your post to make one for you.
If the original text is:

The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he, who in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother’s keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee.

The generated excerpt would become:

The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he, who in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother’s keeper and the finder of… Read More

It’s great when you’re lazy like me, but what’s not so great is IT DOESN’T work with Chinese text, because it uses the spaces in your sentence to define words, since Chinese text doesn’t use spaces like that, it doesn’t work. The only solution I’ve found is to use the following instead of “the_excerpt();”

<?php
 $content = get_the_excerpt();
 $trimmed_content = mb_substr($content, 0, 54)."<a href=". get_permalink() ."> ...Read More</a>";
 echo $trimmed_content;
 ?>

This works by first retrieving the original excerpt, using the mb_substr PHP function to get the first 55 characters from the excerpt and link it with “…Read More”. If you would like a longer excerpt, simply change 54 (In programming, you start counting from zero) to your desired number.
The main flaw to this function is that if you mix chinese and english articles it won’t be able to tell the difference and end up only giving you the first 55 characters. So that will become

The path of the righteous man is beset on all sides by… Read More

If you have a better way to tackle this, please do share it with the rest of us.
Credits to this Stack Exchange post and yes that is a quote from Pulp Fiction 🙂