Blog

Limit characters for post content for WordPress

Posted on: 29 December 2015     Posted by: Freesize Workroom
Categories: Wordpress

A simple function to limit characters for post excerpt, post title and post content for WordPress

The function will make use of WordPress in-built function to strip away short codes (strip_shortcodes()) and HTML tags (wp_strip_all_tags()) and return a string with length not more than the limit set.

Coding for the limit characters function:


function limit_str($string="", $limit=100){

   $string = wp_strip_all_tags(strip_shortcodes($string));

   if(strlen($string)>$limit){
      return substr($string, 0, $limit-3) . "...";
   }else{
      return $string;
   }
}

Simply place the function in your function.php and call it within your template.