How to improve excerpt function in WordPress?

1788

How are you all? I hope all of you are fine. Today I sat to write with which subject that is “how to improve excerpt function“. Those who have missed for them, I gave a link of my previous posts.

How to Customize WordPress Excerpts Function?

wordpress-excerpt

The excerpt function is very important and it is very popular to theme developer. But it can be made more perfect in many parts if you want. In today’s tutorial I will show you how to improve the excerpt function which will not show any other sentence except particular r number and will not be finished in half line.

For this first which work you have to do that is, you have to make a new function. For this open functions.php file of your theme file and paste following code.

// Variable & intelligent excerpt length.
function print_excerpt($length) { // Max excerpt length. Length is set in characters
global $post;
$text = $post->post_excerpt;
if ('' == $text) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
}
$text = strip_shortcodes($text); // optional, recommended
$text = strip_tags($text); // use '
$text = strip_tags($text,'<p><a>'); ' if you want to keep some tags
$text = substr($text,0,$length);
$excerpt = reverse_strrchr($text, '.', 1);
if($excerpt) {
echo apply_filters('the_excerpt',$excerpt);
} else {
echo apply_filters('the_excerpt',$text);
}
}
// Returns the portion of haystack which goes until the last occurrence of needle
function reverse_strrchr($haystack, $needle, $trail) {
return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
}

Your work is finished after saving where you need to show your excerpt function, use there like following code.

I hope this post will work for you. Thank you for reading this article. If there is any mistake, then forgive me. If you face any problem, then don’t forget to comment. If you think the article is beneficial then obviously share it.

Thank You…

Also Read: