2013-02-18 3 views
2

У меня есть этот код, который запускает выдержку за пределами цикла в wordpress.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, но, очевидно, я не знаю, куда поместить код, чтобы он работал. ты можешь помочь мне с этим?

+0

вероятно прямо перед возвращением() вызов ... –

ответ

1

Try:

<?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; 
} 
?> 
+1

просто добавить в свой ответ $ the_excerpt = str_replace (массив («\ n "," ","
"," \ r "), '', the_excerpt()); –

+1

спасибо, много сработало !! благодаря!! :) – Adamo

+0

@Adamo проблем нет. :) –

0

Попробуйте

<?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; 
    } 
?> 
Смежные вопросы