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?
- How to force a WordPress site to redirect HTTP to HTTPS for securing the whole site?,
- If the search results are only in one post, then how to redirect in that post?,
- How to upgrade WordPress comparatively in slow server?,
- How to show Author’s Posts Only in Author Dashboard in WordPress?,
- How to remove the URL field from the WordPress tuments form?,
- How to migrate a WordPress site from a host to another host with the plugin?
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:
-
Some Tips for increasing the loading speed of Your WordPress Site,
-
How to Publish Article from MS Word to WordPress Blog or Blogger Website?,
-
How to Install WordPress Plugins,
-
How to Install WordPress Theme,
-
What to do after installing the WordPress CMS into the server?,
-
How to add Google authorship to your WordPress blog.