
In WordPress, switching from full posts to excerpt is easy if you edit your theme files. But do you know that you could achieve the same result without editing any of your theme file? Just read this recipe to find out.

In WordPress, switching from full posts to excerpt is easy if you edit your theme files. But do you know that you could achieve the same result without editing any of your theme file? Just read this recipe to find out.
What you have to do is to paste this code into your functions.php file and let WordPress filters do the work for you.
function my_excerpts($content = false) {
// If is the home page, an archive, or search results
if(is_front_page() || is_archive() || is_search()) :
global $post;
$content = $post->post_excerpt;
// If an excerpt is set in the Optional Excerpt box
if($content) :
$content = apply_filters('the_excerpt', $content);
// If no excerpt is set
else :
$content = $post->post_content;
$excerpt_length = 55;
$words = explode(' ', $content, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '...');
$content = implode(' ', $words);
endif;
$content = '<p>' . $content . '</p>';
endif;
endif;
// Make sure to return the content
return $content;
}
add_filter('the_content', 'my_excerpts');
Thanks to Justin Tadlock for this cool snippet!
17 Responses
Wow. What a long and useless way to do something that can be achieved by editing one line of code.
Eric B. , you’re right, but this snippet also works if you haven’t set an excerpt or added a ‘read more’ tag at your articles.
Maybe it should be a hook to the get_excerpt() function, but this works just as good.
Just because some people can lick themselves doesn’t mean it’s a good idea! A bit like this piece of code maybe?
I take that back, maybe there is a use with child themes… still unsure about the licking thing though?
I think this code is totally valid. Yeah it’s easier to modify the theme files, but for child themes this is perfect, as an intro to filters it’s a relatively simple example.
Ben explained it all perfectly
The fact it is not the easiest way doesn’t mean it is useless.
What would make this really unique and useful is if it outputted only the first paragraph. Say you have a three paragraph post, it would find the first paragraph by finding the p tags and add the read more after that. I have been looking and working on a way to do that forever!
@Montana: You can easily adept this function for your needs.
Just change the code under // If no excerpt is set
$content = $post->post_content;
if(preg_match_all(‘/(.*)/i’, $content, $matches)) {
$content = ” . $matches[1]. ”;
}
PS I haven’t tested it, but it should give you an idea of what I mean.
Thanks for the code that really works
Thanks for sharing. But instead I use Thumbnail for experts.
To those that are saying to “modify the theme files” instead of adding chunks of code like this into your functions file I say this “pfft” what if over the time you have, 50? maybe more modifications to a theme?
And then you get the inevitable, BAM a major upate is released to your theme? what are you going to do then?
Install the update? and have to do all that work over again?
Or ignore the update? and have a potential security risk on your server?
It is far better to add code like this to your functions file, and make a few changes as posible to your theme files.
When I add this to my function.php file do I need to add it between ?
Sorry my comment did not format right before.
When I add this to my function.php file do I need to add it between
Trackbacks: