Changing the excerpt length in WordPress 2.9 isn’t hard at all. You’ll need to add some php to your functions.php to make this happen.
I’ve had to modify this to work in wordpress version 2.9 and have tested it up to wordpress 2.9.1
add the following to your functions.php right below the <?php at the top of the functions.php file
[php]
function my_wp_trim_excerpt($text) { // Fakes an excerpt if needed
if ( ” == $text ) {
$text = get_the_content(”);
$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);
$text = strip_tags($text, ‘
‘);
$excerpt_length = 50;
$words = explode(‘ ‘, $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, ‘ … READ MORE‘);
$text = implode(‘ ‘, $words);
}
}
return $text;
}
remove_filter(‘get_the_excerpt’, ‘wp_trim_excerpt’);
add_filter(‘get_the_excerpt’, ‘my_wp_trim_excerpt’);[/php]
Change $excerpt_length = apply_filters(‘excerpt_length’, 35);
where 35 is the number of words you want in your excerpt
Sergei Plaxienko says
Does not work. At all.
Kilroy says
Works for me.. what version of WP do you use? Also paste a copy of your functions.php here and I’ll see if I can figure out whats up.. chances are you didn’t use the correct syntax.
CK says
It does not work due to the fact that the code in quotes is rendered oddly and some of your symbols are messed. example: line 3 is suppose to have 2 single quotes, not a double quote (this goes for some another case in your code) and there is a > instead of a >. You’re also missing the permalink values in the array_push so it pulls the right link. The fix below worked for me, but I still can’t get rid of the original Read More generated by wordpress even though the script says it should remove it.
Here’s the updated code with the corrections:
function my_wp_trim_excerpt($text) { // Fakes an excerpt if needed
$raw_excerpt = $text;
if ( ” == $text ) {
$text = get_the_content(”);
$text = strip_shortcodes( $text );
$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);
$text = strip_tags($text);
$excerpt_length = apply_filters(‘excerpt_length’, 55);
$words = explode(‘ ‘, $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, ‘ … Read More …‘);
$text = implode(‘ ‘, $words);
}
}
return apply_filters(‘wp_trim_excerpt’, $text, $raw_excerpt);
}
remove_filter(‘get_the_excerpt’, ‘wp_trim_excerpt’);
add_filter(‘get_the_excerpt’, ‘my_wp_trim_excerpt’);
Anyone know the fix to remove the original excerpt? remove_filter() isn’t working in the functions.php.
CK says
Darn rendering. Could the host edit this post so that the content is in stripped out code instead of html rendered copy? Thx
Kurt says
@CK
I have updated the code – thanks
Steven Fine says
@Kurt
Kurt – I want to get rid of the exerpts all together.
I have tried everything without luck
If I put the $excerpt_length = apply_filters(‘excerpt_length’, 0) will it work ?
me says
In wordpress 2.9 skip all that other stuff and just do this:
// change default length of the_excerpt from 55
function new_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');
// change excerpt more [...]
function new_excerpt_more($post) {
return '...more';
}
add_filter('excerpt_more', 'new_excerpt_more');
Daryl Łomża says
It’s excellent site, I was looking for something like this
Fred says
Hello i have a problem with this function.
The texte of my link is appear in top of my single page…
Can you help me please?
This is my function :
‘, ‘]]>’, $text);
$text = strip_tags($text);
$excerpt_length = 42; // Changes excerpt character count
$words = explode(‘ ‘, $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, ‘Read Full Post →‘); // Changes link text
$text = implode(‘ ‘, $words);
}
}
return $text;
}
?>
Thinks a lot.
PS : this is the page where you can see the probleme
http://actu.loiregrafix.fr/un-site-vitrine-pour-la-fonderie-plomb-navy-lest
Kurt Turner says
Try moving the code around within your .php.. also wrap it in a DIV and format as necessary.