2012-03-08 32 views
0

目前我有一個功能,從而縮短標題的過濾器,如果他們比25characters長,並追加了三個點「...」IF條件滿足執行功能 - WordPress的

然而,追加點所有的標題。我怎樣才能得到以下代碼才能運行只有標題長度超過25個字符?

function japanworm_shorten_title($title) { 
    $newTitle = substr($title, 0, 25); // Only take the first 25 characters 

    return $newTitle . " …"; // Append the elipsis to the text (...) 
} 
add_filter('the_title', 'japanworm_shorten_title', 10, 1); 

回答

2

您可以使用PHP的strlenhttp://php.net/manual/en/function.strlen.php

您的代碼會是這樣的:

function japanworm_shorten_title($title) { 
if (strlen($title) > 25){ 
$title = substr($title, 0, 25).'…'; 
} 
return $title; 
} 

add_filter('the_title', 'japanworm_shorten_title', 10, 1); 
+0

這就是完美!非常感謝你! – Miro 2012-03-08 13:07:46