How to set character limit on the_content() and the_excerpt() in wordpress

后端 未结 9 1342
臣服心动
臣服心动 2020-12-07 23:36

How do I set a character limit on the_content() and the_excerpt() in wordpress? I have only found solutions for the word limit - I want to be able to set an exact amount cha

9条回答
  •  死守一世寂寞
    2020-12-08 00:03

    just to help, if any one want to limit post length at home page .. then can use below code to do that..

    the below code is simply a modification of @bfred.it Sir

    add_filter("the_content", "break_text");
    
    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) . "... read more";
      }else{
        return $text;
      }
    
    }
    

提交回复
热议问题