2012-03-08 111 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