Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGabriel Ward
20,222 Pointsexcerpt length
If I use the code relating to changing the excerpt length shown in this video, it doesn't seem to work in the theme I'm trying to customise. I looked up how to change the excerpt length and came across this code on a thread in stackexchange. It works but it's from 6 years ago and I imagine isn't best practice or up to date or anything. Any thoughts on how I can get the much short and up to date code in this video to run would be greatly appreciated.
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'custom_trim_excerpt');
function custom_trim_excerpt($text) { // Fakes an excerpt if needed
global $post;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text);
$excerpt_length = 5;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, '...');
$text = implode(' ', $words);
}
}
return $text;
}
2 Answers
Jacobus Hindson
14,429 PointsHello Gabriel,
You can add a function (to functions.php) as follows;
<?php function new_excerpt_length( $length ) {
return 30;
}
add_filter( 'excerpt_length', 'new_excerpt_length', 999 ); ?>
The 'return xx;' is the word value you wish to return, it is set to 55 by default.
Of course you can always look at the Codex for more help.
Leandro Incetta
6,106 PointsHi all!
Thank you very much for your input --this is an issue I've been struggling with, too.
I'm developing a WordPress theme from scratch, and unfortunately none of the options listed above worked for me. However, I found a solution writing the function this way:
function custom_excerpt_length( $length ) {
return 30;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
(Here's the link is where I got it from)
Please tell me if it works for you. Thanks!
Gabriel Ward
20,222 PointsGabriel Ward
20,222 PointsHI Jacobus,
Thanks for your answer. This code doesn't seem to alter the excerpt length. However I've found that this code does work:
I'm really not sure why this works and yours doesn't, so any thoughts you've got I'd be interested to hear. I think the 'att' implies Bootstrap?
Jacobus Hindson
14,429 PointsJacobus Hindson
14,429 PointsGood to know, I took mine fairly well from the codex but nice looking block I will grab it for my own use.
Thanks.