简体   繁体   English

修剪用户在content.php wordpress文件中输入摘录

[英]Trim user entered excerpt in content.php wordpress file

I have a custom function to trim the excerpt: 我有一个自定义功能来修剪摘录:

function custom_excerpt_length( $length ) {
return 10; }

and the filter to apply it: 以及应用它的过滤器:

add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

and then I get the excerpt in my content.php as: 然后我在content.php中摘录为:

<?php the_excerpt(); ?>

The function works if not custom user entered excerpt is found. 如果未找到自定义用户输入的摘录,则该功能有效。 so basically grabs the first 10 words from the blog post content. 因此基本上可以从博客文章内容中获取前10个字。 But it doesn't trim when I custom enter the excerpt. 但是当我自定义输入摘录时,它不会修剪。 Any ideas to display the trimmed version of the user entered excerpt in the blog post page? 在博客文章页面上输入摘录的任何想法来显示用户的修剪版本?

I finally got what I needed. 我终于得到了我所需要的。 So I just wanted to reduce the trimmed excerpt to a lower character count. 所以我只想将修剪的节录减少到更少的字符数。 I just added to my functions page: 我刚刚添加到我的功能页面:

function custom_excerpt_more( $more ) {
    $more = '&hellip;';
    return $more; 
}
add_filter( 'excerpt_more', 'custom_excerpt_more' ); //displays ... after the excerpt

function custom_excerpt_length( $length ) {
return 30; //number of words to display in the excerpt
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 ); //999 si it filters at the end

And then in my content.php page: 然后在我的content.php页面中:

<p><?php echo substr(get_the_excerpt(),0,80);?></p>

That way I get no more than 80 characters in my blog post list page and 30 words on my individual blog post page (content-single.php). 这样,我在博客文章列表页面中得到的字符数就不会超过80个,而在个人博客文章页面(content-single.php)中得到的字符数却不超过30个。

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

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