title: Retain formatting while using excerpt [ wp_trim_words() ]
body: <p>I am using <code>&lt;?php echo wp_trim_words(get_the_content(), 100); ?&gt;</code> 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.</p>
  
  <p>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 ?</p>
  
  <p>Thanks</p>
  
  <p><strong>I found a solution to this may be it can help others as well.</strong></p>
  
  <p><code>function content_excerpt($excerpt_length = 5, $id = false, $echo = true) {
      $text = '';
      if($id) {
          $the_post = &amp; get_post( $my_id = $id );
          $text = ($the_post-&gt;post_excerpt) ? $the_post-&gt;post_excerpt : $the_post-&gt;post_content;
      } else {
          global $post;
          $text = ($post-&gt;post_excerpt) ? $post-&gt;post_excerpt : get_the_content('');
      }
      $text = strip_shortcodes( $text );
      $text = apply_filters('the_content', $text);
      $text = str_replace(']]&gt;', ']]&amp;gt;', $text);
          $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
          if ( count($words) &gt; $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;
  }</code></p>
  
  <p><code>function get_content_excerpt($excerpt_length = 5, $id = false, $echo = false) {
   return content_excerpt($excerpt_length, $id, $echo);
  }</code></p>
  
  <p><code>// Call function in template file via</code></p>
  
  <p><code>&lt;?php content_excerpt(50); // 50 is amount to words ?&gt;</code></p>