2013-02-18 3 views
2

J'ai ce code qui exécute l'extrait en dehors de la boucle dans wordpress.Wordpress supprimer des espaces blancs

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

Comme vous pouvez le voir supprime tout le contenu comme des images et d'autres choses et ne laisse que le texte, mais quand il le fait, il génère aussi beaucoup d'espaces blancs (retours). J'ai essayé de les supprimer avec \ n, \ t, \ r mais évidemment je ne sais pas où mettre le code pour le faire fonctionner. Peux-tu m'aider avec ceci?

+0

probablement juste avant le retour() appeler ... –

Répondre

1

Essayez:

<?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

juste ajouter à votre réponse $ the_excerpt = str_replace (array (« \ n "," ","
"," \ r "), '', the_excerpt()); –

+1

merci beaucoup, cela a fonctionné parfaitement !! Merci!! :) – Adamo

+0

@Adamo Pas de problème. :) –

0

Essayez cette

<?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; 
    } 
?> 
Questions connexes