Retain formatting while using excerpt [ wp_trim_words() ]

I am using <?php echo wp_trim_words(get_the_content(), 100); ?> in my template file to control amount of words to be displayed on a page and also have a link below it for taking user to the next page to read entire content but this function removes all the formatting of content on preview page.

I can’t use wordpress default excerpt function here as it is being used elsewhere and i need this to be of different length than that. Is there a way to retain formatting while using this ?

Read More

Thanks

I found a solution to this may be it can help others as well.

function content_excerpt($excerpt_length = 5, $id = false, $echo = true) {
$text = '';
if($id) {
$the_post = & get_post( $my_id = $id );
$text = ($the_post->post_excerpt) ? $the_post->post_excerpt : $the_post->post_content;
} else {
global $post;
$text = ($post->post_excerpt) ? $post->post_excerpt : get_the_content('');
}
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]&gt;', $text);
$words = preg_split("/[nrt ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
if ( count($words) > $excerpt_length ) {
array_pop($words);
$text = implode(' ', $words);
$text = $text . $excerpt_more;
} else {
$text = implode(' ', $words);
}
if($echo)
echo apply_filters('the_content', $text);
else
return $text;
}

function get_content_excerpt($excerpt_length = 5, $id = false, $echo = false) {
return content_excerpt($excerpt_length, $id, $echo);
}

// Call function in template file via

<?php content_excerpt(50); // 50 is amount to words ?>

Related posts

Leave a Reply

2 comments

  1. To anyone else facing similar issues, I took the default wp_trim_words() function directly out of core, and altered it to not strip tags:

    function wp_trim_words_retain_formatting( $text, $num_words = 55, $more = null ) {
                        if ( null === $more )
                            $more = __( '&hellip;' );
                        $original_text = $text;
                        /* translators: If your word count is based on single characters (East Asian characters),
                           enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
                        if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf-?8$/i', get_option( 'blog_charset' ) ) ) {
                            $text = trim( preg_replace( "/[nrt ]+/", ' ', $text ), ' ' );
                            preg_match_all( '/./u', $text, $words_array );
                            $words_array = array_slice( $words_array[0], 0, $num_words + 1 );
                            $sep = '';
                        } else {
                            $words_array = preg_split( "/[nrt ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
                            $sep = ' ';
                        }
                        if ( count( $words_array ) > $num_words ) {
                            array_pop( $words_array );
                            $text = implode( $sep, $words_array );
                            $text = $text . $more;
                        } else {
                            $text = implode( $sep, $words_array );
                        }
                        /**
                         * Filter the text content after words have been trimmed.
                         *
                         * @since 3.3.0
                         *
                         * @param string $text          The trimmed text.
                         * @param int    $num_words     The number of words to trim the text to. Default 5.
                         * @param string $more          An optional string to append to the end of the trimmed text, e.g. &hellip;.
                         * @param string $original_text The text before it was trimmed.
                         */
                        return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
                    }