fatBuzz of Fatbuzz Development Team
12/18/2017 - 10:50 AM

Truncate post excerpt to fixed number of characters

Add truncate.php to your themes functions.php file, update the third argument in line 8 to change the number of characters.

Add new-excerpt.php to the themes templates to output as required.

<p class="excerpt"><?php echo get_excerpt();  ?></p>
<?php

function get_excerpt(){
   $excerpt = get_the_content();
   $excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
   $excerpt = strip_shortcodes($excerpt);
   $excerpt = strip_tags($excerpt);
   $excerpt = substr($excerpt, 0, 50);
   $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
   $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
   $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
return $excerpt;
}