neon_dannii
Member
Posted 4 weeks ago #
I've got the latest version of wordpress installed.
I'm writing text into the excerpt box in the 'add new post' box of the admin system and then calling this via the_excerpt() function.
I've tried several ways of changing the default excerpt length from 55 to 10 words, including changing the excerpt_length in the formatting.php file, as well as adding the well known functions within my own theme's functions template that I found in the codex.
Can anyone give me any pointers or codes where they have been successful in trimming the excerpt length?
Much appreciated!
Put this in your functions.php
/* Create excerpt with 10 words and preserved HTML tags */
function my_wp_trim_excerpt($text) {
{
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = 10;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, '[...]');
$text = implode(' ', $words);
}
}
return $text;
}
// remove WordPress default excerpt filter
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
// Add our custom filter with low priority
add_filter('get_the_excerpt', my_wp_trim_excerpt, 20);
Peter