Sometimes we need to limit how many words will be displayed in our excerpt, If you have use WordPress 2.9.x you can use Easy Excerpt method, but if you want something different, you can create own excerpt and replace your original excerpt.
To create own excerpt in your WordPress, write down this code below in your functions.php
<?php add_filter('the_excerpt', 'my_excerpts');
function my_excerpts($content = false) {
global $post;
$mycontent = $post->post_excerpt;
$mycontent = $post->post_content;
$mycontent = strip_shortcodes($mycontent);
$mycontent = str_replace(']]>', ']]>', $mycontent);
$mycontent = strip_tags($mycontent);
$excerpt_length = 55;
$words = explode(' ', $mycontent, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '...');
$mycontent = implode(' ', $words);
endif;
$mycontent = '<p>' . $mycontent . '</p>';
// Make sure to return the content
return $mycontent;
}
?>
For some reason, if you want to keep original excerpt and only need this code to display on special place, you can remove the add_filter functions and then place this code in whatever you want it. Please note this code must be placed in The Loop.
<?php echo my_excerpts(); ?>
You have done modified original excerpt with your custom excerpt in WordPress. Original Idea, come from Justin Tadlock




I am such a newbie to MySQL and have really enjoyed your tips and tricks!! I really need to study the code more!! I love the idea of your excerpt function but have received the error … Parse error: syntax error, unexpected ‘<', expecting T_VARIABLE or '$' … any thoughts? I am developing my own theme and am using WP 2.9.1. Hmmm…do you happen to have code for logging in users? your code is so clean and I prefer using code to plugins. One other I am looking for is the code for auto applying tags to comments and posts based upon check boxes at login time. I would store this info in DB. Thanks for any tips you have to offer! Leslie
Try to View Code then copy from that page and give me feedback, does the error still appear?
This is the line 14 where my error occurs…$content = str_replace(‘]]>’, ‘]]>’, $content);
When I view source, this is what I found…
Parse error: syntax error, unexpected '<', expecting T_VARIABLE or '$' in C:\xampplite\htdocs\wordpress\wp-content\themes\somethinggood\functions.php on line 14
Thank you for your help
I am really sorry, this code was auto rewriten by this plugin “SyntaxHighlighter Evolved” but I was fix it right now, please use my latest code that displayed on the post above.
If you still got any problem, please let’s me know
Pingback: How to modify Custom excerpt length on WordPress # WordPress Tricks & Tips
stupid tutorial, won’t even work!
What’s you mean doesn’t work? what’s error that you got?
we can create own excerpt and replace your original excerpt through this blog. I would store more information from this.
you need to get rid of “” at the beginning and the end of the script before pasting it into functions.php.
i guess that´s why a lot say it´s not working
Is the best way to display excerpts to use a Plug In – or to use an inbuilt function of WordPress?
TIA.
paul
line 7 should be strip_shortcodes($content);
is that you mean?
strip_shotcodes($mycontent);There are a handful of WordPress Excerpt Plugins out there for those who are not comfortable editing the .php files in the backend.
Here is one that I currently use…
http://wordpress.org/extend/plugins/advanced-excerpt/
Thanks for this brilliant solution. My theme didn’t accept the specialized excerpt_length, but with this it’s working fine. =)
Best greetings
could you write me how will the whole code look like, if you want to create a new excerpt tipe? (not replacing the custom one)
Pingback: 20 More WordPress Code Snippets and Hacks
Pingback: 20 More WordPress Code Snippets and Hacks | The best Tutorials
Pingback: 20 More WordPress Code Snippets and Hacks | FlexLib
Pingback: 20 More WordPress Code Snippets and Hacks | Wbcom Designs | VAPDI
Pingback: 20 More WordPress Code Snippets and Hacks « Free UK Web Design
Pingback: 20 More WordPress Code Snippets and Hacks | Preukson
Pingback: 20 More WordPress Code Snippets and Hacks- rampantdesigntools.com
Pingback: 20 More WordPress Code Snippets and Hacks | Programming Blog
Pingback: 20 More WordPress Code Snippets and Hacks | Typography Blog
I just miss programming. This is one sure of a professional work.