Problem:
sometimes on the home page we would like to show the excerpt rather than the whole content of a post.
WordPress generates an excerpt automatically by selecting the first 55 words of the post .
But I'm having trouble using it on a multi-lingual site (Eng/Chinese).
The {excerpt} formatting tag only allows you to truncate it's length based on "words" not on "character count" -
but due to the nature of Chinese, "words" are not formatted in the same way (with a space between them).
So, I could have a chunk of Chinese text that has a hundred words in it, but if I put {excerpt:50}
it will display it all, because it is not actually hunting for "words" it is hunting for "spaces".
Solution:
Firstly i want to solve the problem with some functions in wordpress.
I thought i need to control the length of excerpt.
http://www.lancelhoff.com/change-the-excerpt-length-wordpress/
http://stuartduff.com/limit-wordpress-posts-text-length-without-the-use-of-plugins-16122009
http://www.wpinsideout.com/five-ways-to-control-the-length-of-your-wordpress-excerpt
Later on i found that it is not a problem of length of excerpt,because it works fine with english, but a problem of multi-lingual (chinese).
So i resorted to some plugins that trims the excerpt to a given length using either character count or word count .
http://sparepencil.com/portfolio/advanced-excerpt/
The tricky thing is the plugin above works well with less that 20 character s but not well with more than 50. So i give it up and reconsider the whole thing.
At last,i read the code on what cause the problem and came up with the solution:
Using php function strlen() and substr()
When you get the whole content,use strlen() to calculate the length of the string.
And use substr() to select the string you want to show on the page.
For example
if (strlen( bp_get_activity_content_body ())> 500 )
{
echo substr( bp_get_activity_content_body (), 0 , 500 );
echo "[...]" ;
}
If you have any other solution that can tackle this problem,feel free to tell me,i am all ears.
Reference:
http://codex.wordpress.org/Excerpt
http://codex.wordpress.org/Template_Tags/the_excerpt
http://op111.net/67
http://wordpress.org/support/topic/similar-posts-excerpt-and-multibyte-chinese-characters
Thoughts:
1.When i came across a tough problem i firmly believe that there is several solutions rather than only one
for the problem.i need to consider it in different perspective.
2. Find the reasons that cause the problem is the key to find the solutions.