Limit Excerpt Text

October 17, 2014 (6 years ago)
Uncategorized

When you have layout areas with specific sizes, you need to limit the amount of text that appears. Rather than modify the excerpt size for the whole site, you can use this function:

function lt_limit_words($string, $max){
   $words = explode(' ', $string);
   $count = count($words);
   $words = implode(' ', array_slice($words, 0, $max));

   if($count > $max){
   // make the 'Read More' match your regular excerpts
      return $words . '<br><a class="moretag" href="'. get_permalink($post->ID) . '"> [Read More...]</a>';
   } else {
      return $string;
   }
}

 Then, call it in your template (from inside the loop) like this:

<div id="block-content">
   <?php echo lt_limit_words(get_the_content(), 25); ?>
</div>

 

Skip to toolbar