简体   繁体   中英

How can I limit the number of characters in an excerpt?

I'd like your help to fix something that's driving me crazy. In my front-page.php I have modified my excerpts length with this code:

//* Modify the length of post excerpts
add_filter( 'excerpt_length', 'rtny_excerpt_length' );
function rtny_excerpt_length( $length ) {
    return 60; // pull first 60 words
}

The problem is that if I limit the length with words (as some words are longer than other), the result in the web I'm creating is not looking so well this is how it looks like .

I think if I could limit the number of characters or lines instead of words I could get a better look with all meta aligned. But I'm really new in coding so I pleaaase really need help. I hope my English is not so bad and :)

Thank you in advance!

Here is a nice excerpt function that takes tags into account:

function get_excerpt(){
$excerpt = get_the_content();
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 40);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
return $excerpt;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM