显示博文中的前140个字符

时间:2014-12-13 12:07:36

标签: wordpress plugins wordpress-plugin

我希望从帖子内容中获得前140个字符并将其显示在主页中。我为它创建了插件,并没有给我带来的愿望。你能告诉我我在哪里犯了错误,我怎样才能恢复它?这是我在插件文件中编写的代码

$length = 140;
function cp_trim_content($length){
$content = explode(' ', get_the_content(), $length );

if( count($content) >= $length)
{
array_pop($content);
$content = implode (" ", $content).'...';
} 
else 
{
$content = implode (" ", $content);
}
//$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
return $content; 
}

1 个答案:

答案 0 :(得分:0)

如果您想要140个字符,那么以下代码将适合您

    $length = 140;
    function cp_trim_content($length)
    {
        $content = get_the_content();
        if(strlen($content)>$length)
        {
          $final_content = substr($content,0,140).' ...';
        } 
        else
        {
          $final_content = $content;
        }
        return $final_content; 
    }