This question already has answers here: Closed 12 years ago.admin管理员组文章数量:1332395
Possible Duplicate:
excerpt in characters
I have a question after reading this post (How to highlight search terms without plugin). I like this function(Search Term Without Plugin) very much but the character length is too long. What php code should i add to make the excerpt shorter? Would appreciate if someone can suggest it. Thank you!
This question already has answers here: Closed 12 years ago.Possible Duplicate:
excerpt in characters
I have a question after reading this post (How to highlight search terms without plugin). I like this function(Search Term Without Plugin) very much but the character length is too long. What php code should i add to make the excerpt shorter? Would appreciate if someone can suggest it. Thank you!
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Oct 30, 2012 at 4:47 JornesJornes 7535 gold badges12 silver badges31 bronze badges 02 Answers
Reset to default 22add these lines in function.php file
function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
In addition to the above filter hook supplied by Deepa's answer here is one additional function that can help you extend the use of the_excerpt
in two ways,
Allows you to...
Limit the excerpt by number of characters but do NOT truncate the last word. This will allow you to return a maximum number of characters but preserve full words, so only the words that can fit within the specified number limit are returned and allow you to specify the source of where the excerpt will come from.
function get_excerpt($limit, $source = null){
$excerpt = $source == "content" ? get_the_content() : get_the_excerpt();
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, $limit);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = $excerpt.'... <a href="'.get_permalink($post->ID).'">more</a>';
return $excerpt;
}
/*
Sample... Lorem ipsum habitant morbi (26 characters total)
Returns first three words which is exactly 21 characters including spaces
Example.. echo get_excerpt(21);
Result... Lorem ipsum habitant
Returns same as above, not enough characters in limit to return last word
Example.. echo get_excerpt(24);
Result... Lorem ipsum habitant
Returns all 26 chars of our content, 30 char limit given, only 26 characters needed.
Example.. echo get_excerpt(30);
Result... Lorem ipsum habitant morbi
*/
This function can be used multiple times through out theme files, each with different character limits specified.
This function has the ability to retrieve an excerpt from either,
the_content
the_excerpt
For example, if you have posts that contain text in the_excerpt box on the post editor screen, but want to pull an excerpt from the_content body instead for a special use case you would instead do;
get_excerpt(140, 'the_content'); //excerpt is grabbed from get_the_content
This tells the function that you want the first 140 characters from the_content
, regardless of whether an excerpt is set in the_excerpt
box.
get_excerpt(140); //excerpt is grabbed from get_the_excerpt
This tells the function that you want the first 140 characters from the_excerpt
first and if no excerpt exists, the_content
will be used as a fallback.
The function can be improved to be made more efficient and or incorporated with the use of WordPress filters for both the_content
or the_excerpt
or simply used as is in situations where there is no suitable, in-built WordPress API alternative.
本文标签: How can i limit the character length in excerpt
版权声明:本文标题:How can i limit the character length in excerpt? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742265027a2443226.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论