Wordpress Titles: If Longer Than 50 Characters, Show Ellipsis

前端 未结 9 1337
谎友^
谎友^ 2021-02-01 19:06

I have a wordpress site with titles, and if the title has more than 50 characters I need to add an ellipsis (...) at the end of the title and stop the title at 50 characters. Be

9条回答
  •  执念已碎
    2021-02-01 19:20

    Add this to your "functions.php" file in your theme folder....

    function the_title_excerpt($before = '', $after = '', $echo = true, $length = false) 
      {
        $title = get_the_title();
    
        if ( $length && is_numeric($length) ) {
            $title = substr( $title, 0, $length );
        }
    
        if ( strlen($title)> 0 ) {
            $title = apply_filters('the_title_excerpt', $before . $title . $after, $before, $after);
            if ( $echo )
                echo $title;
            else
                return $title;
        }
    }
    

    then call the title like as follows

    
    

提交回复
热议问题