Support » Fixing WordPress » 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_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,

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