Wordpress删除空白


Wordpress remove white spaces

我有这段代码,它在wordpress中的循环外运行摘录。

<?php
function get_excerpt_by_id($post_id){
$the_post = get_post($post_id); //Gets post ID
$the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
$excerpt_length = 35; //Sets excerpt length by word count
$the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
$words = explode(' ', $the_excerpt, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '…');
$the_excerpt = implode(' ', $words);
endif;
$the_excerpt = '<p>' . $the_excerpt . '</p>';
return $the_excerpt;
}
?>

正如你所看到的,它删除了所有的内容,比如图像和其他东西,只留下文本,但当它这样做时,它也会输出很多空白(返回)。我试着用''n,''t,''r删除它们,但很明显,我不知道把代码放在哪里才能使其工作。你能帮我做这个吗?

尝试:

<?php
function get_excerpt_by_id($post_id){
$the_post = get_post($post_id); //Gets post ID
$the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
$excerpt_length = 35; //Sets excerpt length by word count
$the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
$words = explode(' ', $the_excerpt, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '…');
$the_excerpt = implode(' ', $words);
endif;
$the_excerpt = '<p>' . $the_excerpt . '</p>';
// LOOK HERE
$the_excerpt = str_replace("'r", "", $the_excerpt); // Replace carriage returns
$the_excerpt = str_replace("'n", "", $the_excerpt); // Replace new lines
// .. etc
return $the_excerpt;
}
?>

试试这个

<?php
    function get_excerpt_by_id($post_id){
       $the_post = get_post($post_id); //Gets post ID
       $the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
       $the_excerpt = apply_filters('the_excerpt', $the_excerpt);
       return $the_excerpt;
    }
?>