简体   繁体   English

如何限制摘录中的字符数?

[英]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: 在我的front-page.php中,我用以下代码修改了摘录的长度:

//* 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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