search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

Limit Post Excerpt Length In English And Chinese Edition For WordPress – ARON HACK 亞倫害的

Orignal Post

Limit Post Excerpt Length In English And Chinese Edition For WordPress


Post preview, also called Excerpt in WordPress. There are some ways to limit the length of except.

The Most Common Way

function custom_excerpt_length( $length ) {
    return 50;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

The most common code is above. However, it comes with another issue, if you defined a custom excerpt, the length of it wouldn’t be limited. In other words, it will show completely.

Limit Post Excerpt Length In English And Chinese Edition For WordPress

The Better Way For Multilingual Sites

The way to calculate words is different in each language. The typical types are alphabet language and character language. The former is like English, and latter is like Chinese. As a result, you can place this code in your template. Notice that I used Polylang to create my multilingual site, so you have to modify the second line if you use other plug-in, like WPML.

// Function from Polylang
$cur_lang = get_bloginfo("language")

// Excerpt Length Limit
if ($cur_lang == 'zh-TW') {
  $limit = 70;
  $excerpt = strip_tags(get_the_excerpt());
  $excerpt = mb_substr($excerpt, 0, $limit);
}elseif ($cur_lang == 'en-US') {
  $limit = 120;
  $excerpt = strip_tags(get_the_excerpt());
  $excerpt = mb_substr($excerpt, 0, $limit);
}

echo '

'.$excerpt.'...

';

And you can test the string length with this code.

echo '

'.mb_strlen('亞倫害的').'

'; // Chinese Version echo '

'.strlen('E-book Reader Intro').'

'; // English Version

Notice that I add … between the p tags, I used it as my read more text. And if you add this code in your function.php, remove it, or … will display twice on your automatically generated excerpts. Refer here, 〈Customize Read More Text In WordPress Excerpt〉.

function custom_excerpt_more($more) {
     return '';
 }
 add_filter('excerpt_more', 'custom_excerpt_more');

Related Posts

The Complete WordPress Customization Tutorial – Just Copy And Paste And Work

Content Protection by DMCA.com


熱門推薦

本文由 aronhackcom 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦