A little known fact about WordPress is that you can easily change how functions act within each theme. You’ve might have noticed the functions.php file when it came packaged with your theme. This file not only handles the way widgets are shown (which you’ve probably edited before), but also the core functions.
I was previously using a plugin titled ‘homepage excerpts’ to show sample text on impNERD’s homepage. However, it wasn’t doing everything I wished. Instead, I decided to change the the_excerpt() function.
![]()
Here’s a simple way to change the_excerpt (or any function):
- Find the function you wish to change within /wp-includes/.
- Copy and paste the entire function into your functions.php file located in your theme folder.
- Change what you wish. This can be as little as a number or as much as the way the entire function works.
- Rename the function to something unique (I usually use something like [theme name]_name_of_original_function.
- Add:
remove_filter('name_of_filter', 'name_of_function_to_change');
add_filter('name_of_filter', 'name_of_new_function');
At the bottom of the page.
You may need to do some research on what filter to change, but most older WordPress functions are well documented on WordPress.org.
Here’s a full example of changing the_excerpt:
function themename_trim_excerpt($text) { // Fakes an excerpt if needed
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = preg_replace('@
Related posts »




















Sam

November 10, 2008 @ 1:28 pm
Hi, I tried this and got this error:
Warning: Missing argument 1. Any idea why?
Gary R. Hess

November 10, 2008 @ 2:30 pm
@Sam you need to give a lot more information than that
Did you just copy and past the function? (If so, you need to change the quote marks to single quotes, wordpress prints them weird). If you did, then your text variable isn’t set for some reason. You can try changing it to $text = '' within the function:
function themename_trim_excerpt($text = '')
Sam

November 10, 2008 @ 2:50 pm
Gary,
Thanks for responding so quickly. I’ve been trying this for a day and lost a couple of patches of hair in the process.
I changed the quote marks and placed the copy and pasted code in my functions folder. Here’s what it currently looks like:
function homepage_excerpt($text = ‘ ‘) { // Fakes an excerpt if needed
if ( ” == $text ) {
$text = get_the_content(”);
$text = strip_shortcodes( $text );
$text = apply_filters(‘the_content’, $text);
$text = preg_replace(‘@.*?@si’, ”, $text); //remove javascript
$text = preg_replace(‘@@’, ‘ ‘, $text); // remove CDATA, html comments
$text = str_replace(‘]]>’, ‘]]>’, $text);
$text = strip_tags($text, ‘ ‘);
$excerpt_length = 25;
$words = explode(‘ ‘, $text, $excerpt_length + 1);
array_pop($words);
array_push($words, ‘[...]‘);
$text = implode(‘ ‘, $words);
}
return $text;
}
remove_filter(‘get_the_excerpt’, ‘wp_trim_excerpt’);
add_filter(‘get_the_excerpt’, ‘homepage_excerpt’);
Basically I am trying to get different excerpt functions working so I can call excerpts of different lengths onto different pages.
(I’m building a magazine CMS version of wordpress and your code seemed the cleanest)
I keep getting this error though:
Warning: Missing argument 1 for homepage_excerpt(), called in /home/site/public_html/testblog/wp-content/themes/pdmag1108/home.php on line 54 and defined in /home/site/public_html/testblog/wp-content/themes/pdmag1108/functions.php on line 565
Gary R. Hess

November 10, 2008 @ 6:08 pm
@Sam you’re right, for whatever reason it doesn’t work for the newest WordPress version. I’ll try to hunt it down.