the_excerpt() vs. the_content()

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.

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值