Title: Improving the Post Excerpt function
Last modified: August 19, 2016

---

# Improving the Post Excerpt function

 *  [zerovic](https://wordpress.org/support/users/zerovic/)
 * (@zerovic)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/improving-the-post-excerpt-function/)
 * hey guys,
    I’ve recently found a great way to improve the default post excerpt
   function with a little code added to my functions.php
 *     ```
       function improved_trim_excerpt($text) {
       	global $post;
       	if ( '' == $text ) {
       		$text = get_the_content('');
       		$text = apply_filters('the_content', $text);
       		$text = str_replace(']]>', ']]>', $text);
       		$text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
       		$text = strip_tags($text, '<p>');
       		$excerpt_length = 40;
       		$words = explode(' ', $text, $excerpt_length + 1);
       		if (count($words)> $excerpt_length) {
       			array_pop($words);
       			array_push($words, '[...]');
       			$text = implode(' ', $words);
       		}
       	}
       	return $text;
       }
       ```
   
 * and also unloading the default function and loading the new one with
 *     ```
       remove_filter('get_the_excerpt', 'wp_trim_excerpt');
       add_filter('get_the_excerpt', 'improved_trim_excerpt');
       ```
   
 * it works great now, since it won’t remove any html codes from the post, so it
   respects the new lines I’m adding to the post.
 * my question is the following…is there a way to make it add everything before 
   the first line break? so it displays the post, but when it reaches the first 
   new line, it stops ? hope you understand 🙂
 * thanks in advance,
    zerovic

The topic ‘Improving the Post Excerpt function’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [zerovic](https://wordpress.org/support/users/zerovic/)
 * Last activity: [15 years, 6 months ago](https://wordpress.org/support/topic/improving-the-post-excerpt-function/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
