strlen() returning 0


strlen() returning 0

如果我做错了什么,请告诉我。 strlen()不断返回 0。

这是我的代码:

$tempstr = the_title();
if (strlen($tempstr) > 12) {
    echo substr($tempstr,0,9) . "...";  
} els e{
    echo $tempstr;
    echo strlen($tempstr);
} 

你的问题是WordPress函数the_title不返回字符串,但echo它。若要以字符串形式获取标题,请使用 get_the_title

$tempstr= get_the_title();
if (strlen($tempstr) > 12){
    echo substr($tempstr,0,9)."...";    
}else{
    echo $tempstr;
    echo strlen($tempstr);
}