Categories: Snippets

Limit character in content and title in wordpress

function break_text($text, $length = 500){
    if(strlen($text)<$length+10) return $text;//don't cut if too short

    $break_pos = strpos($text, ' ', $length);//find next space after desired length
    $visible = substr($text, 0, $break_pos);
    return balanceTags($visible) . " […]";
}

or with readmore

function limit_text($text){

  if(is_front_page())
  {
    $length = 250;
    if(strlen($text)<$length+10) return $text; //don't cut if too short
    $break_pos = strpos($text, ' ', $length); //find next space after desired length
    $visible = substr($text, 0, $break_pos);
    return balanceTags($visible) . "... <a href='".get_permalink()."'>read more</a>";
  }else{
    return $text;
  }

}

 

Recent Posts

Adding or Including custom post type to default WordPress loop

Adding or Including custom post type to default WordPress loop by altering the 'pre_get_posts' function.…

5 years ago

How to Limit or Disable WordPress Post Revisions

Limit the number of posts revisions that WordPress stores in the database. edit wp-config.php file…

5 years ago

Multisite – List Recent Posts Across an Entire Network

In Multisite, List Recent Posts Across an Entire Network by using custom query or WP_Query…

5 years ago

Disable update notification for individual plugins / All

code for the plugin file itself:- [crayon-662cb88226acc032181303/] I like to put this in the actual…

5 years ago

How to change the WordPress permalink structure to remove dates from the URLs

Switching the permalink structure from /year/month/post-slug to just /post-slug is easy by going to Settings…

6 years ago

Hide page visual editor if certain template is selected

[crayon-662cb88226e93540574475/]  

6 years ago