I’ve been working away at a new theme for the past couple of weeks. Actually, it’s a theme framework that I’ve been building upon for the last two months or so, slowly trying to put all the bits and pieces together.
I ran into a bit of a hitch though.
Let’s suppose our front page and archives used the WordPress function <?php the_content(); ?> to retrieve all post content. Let’s further suppose we’re want to modify this with a child theme to show excerpts instead of the entire post.
Yes, we could easily just change <?php the_content(); ?> to <?php the_excerpt(); ?>. But, I don’t want to edit the core files at all. I want to be able to upgrade my theme without my modifications being broken.
Solution
WordPress filters.
In my child theme’s functions.php file, I added this bit of PHP:
// Add filter to the_content
add_filter('the_content', 'my_excerpts');
This tells WP that we want to filter the_content() with our own function.
Here’s what that function looks like:
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;
}
Now, we’ve changed the instances of the_content() that we want to excerpts. This example doesn’t strip the tags of the excerpt like the default WordPress function, but we can easily change that by changing this line:
$content = $post->post_content;
to:
$content = $post->post_content;
$content = strip_shortcodes($content);
$content = str_replace(']]>', ']]>', $content);
$content = strip_tags($content);
There may be a better way to filter the_content(), and I’d love to see what you can come up with if so.

This is awesome; I’ve been looking for a solution to this for a long time. However, I really wish
the_excerpt()had a link to the full article at the end. Since the ‘…’ can be customized here as well, why not add a link?So, that’s how filter works…
Then, it means we can just modifying a theme with functions.php…
Thanks for sharing this…
But if we change the_content with this, how about plug in that add_action to the the_content() ?
Will it still works?
This works like a plugin, Nice trick! That’s why I hate plugins and do like to add everything from theme side.
So, using filters and actions, child theme will work as a machine… Nice tip, friend.
Dan
It’s something I’ve had on my mind for a while too. I just had to sit down and dig through a little of the core WordPress code to see how it works.
You could definitely add a link to the article with this or tweak it however you want. I wanted to keep the tutorial pretty basic to show how we could use filters a bit.
Or, we could just filter
the_excerpt()and add a link at the end.Take a little time to poke around in
/wp-includes/post-template.phpand/wp-includes/default-filters.php.Pangeran
As far as I know, this shouldn’t mess up the plugins, but I’d have to test a few to confirm that. We’re still returning the
$contentafter we’ve filtered it.J Mehmett
Well, to me themes are plugins. They’re just plugins that deal more with the display of your blog.
A theme’s
functions.phpis just a plugin itself too. The more we take advantage of it, the easier it is to keep theme files clean.If I can accomplish something easily enough without a plugin, then I’ll usually add it to the theme.
That’s a cool use of filters, Justin. These days I usually just include a file called ‘loop.php’ with my themes which uses a bunch of conditionals to determine content vs. excerpt depending on the page you’re on. I’m liking the idea of using filters now, though.
Darren
I was torn between using conditional tags within the theme files or filtering it. I’ve tried the
loop.phpapproach before and it works for some projects.Since I’m building this a bit upon a theme framework, which I’ll be working exclusively with for quite some time, I figured I’d use a filter, which would allow me to work with multiple types of layouts without changing the core theme files.
My main goal recently has been to make default theme files as bland and unexciting as possible, which makes it easier for the user in the long run.
Hi Justin,
This was exactly what I was looking for. I was trying to do something like that.
The only problem I had is tha I use a plugin [can't remember the name] that I can add some BBCODE-like on my text and it makes a adsense banner appear. I put something like:
[ad#banner-post]
And it shows the banner.
Using the code you put here, it doesn’t work anymore. Do you have any idea why it stopped work?
Thanks in advance
I was thinking about adding some extra code in your above code, so I could make the links in the post appear in the
the_excerpt();too as they appear in thethe_content();. Because,the_excerpt();doesn’t show links in the post by default. I’ll give it a try.Of course the more we use
functions.phpthe less we need a plugin.Thanks again Justin!! Great work!!!
This would be much better served as a plugin. You could easily set what pages this should effect as well.
Great tip. I will try to implement this.
Thanks..
Sorry guys but Im not a php guy. I follow the abouve procedure and put the code “// Add filter…” and “function my_excerpts…” to mu function.php under my present theme. But could not get it to work.
The article post still displays the whole article. Need help.
oh by the way, does it also filters even old posts? I wanted to reduce ALL articles on my blogs to show only a number of characters. Thanks
impNERD
When you’re working with the concept of child themes, this is much better within the theme. Themes should control how your content is output anyway.
Nihar
No problem.
baldo
This is something that’s really for someone that’s comfortable using PHP, especially in the context of a child theme, and only when the situation warrants it.
It may be better to just use
the_excerpt()instead ofthe_content()within your theme files.The code above definitely works. I’m using it at Theme Hybrid.
Thanks!! This helped a lot, you made my day
Hey Justin,
This looks like the perfect solution to what I want to do. But you will have to forgive me as I am a newbie to the WordPress world. That being said, I am a little frustrated that WordPress’s own documentation and then here doesn’t say *where* to add this code or in what files..? Did I miss something?? As I said I am new, but it is like everybody assume that you know where these functions are!
If you could advise I would quite grateful for the time it will save me sifting through files.
Many thanks in advance!
Actually, it does say where to add this code in the post. There’s this line that reads:
Of course, you could simply add this in your theme’s
functions.phpfile as well if you’re not using parent/child themes.great script, Justin.
i added a linked-to-post ellipsis ($postlink) at the end of the excerpt:
Deleted by admin. Please use character entities when posting code.
to get it w/in the auto-added tags of the “optional excerpt”,
i added the link to your script here::
$content = apply_filters('the_excerpt', $content . $postlink);then i also added it you your “if no excerpt” script here:
$content = '' . $content . $postlink . '';not sure if that’s the best way to do job, but it does work in both cases:
http://hearingvoices.com/news/2008/
oh, forgot, i also removed your ellipsis here:
array_push($words, '');Hi Justin, Is it possible on the excerpt remove the Image only? and keep the other thing on the content just like the original?
This doesn’t seem to work when your front page is a static page and not the blog page… I assume I’ll need to replace
is_front_pagewith something else? but what?I guess I’ll reply to my own question since I figured it out… instead of
is_front_pageI putis_home– that’s in this part of the code in case you are wondering:// If is the home page, an archive, or search resultsif(is_home() || is_archive() || is_search()) :
global $post;
$content = $post->post_excerpt;
This makes blog post excerpts when front page is static and blog page is a different page.
Very GOOD!
Will try to use it!
Thanks
Dude- you’ve saved my bacon. I’ve been struggling with the_content vs. the_excerpt as a n00b for the past few weeks, and your post made the “AHA!” bulb go off-
Thanks very much!
Thanks, I will implement this on my new developed themes. Mike
Hi Justin,
Hope you are all good.
I wonder if you could post a short tutorial on how to swap the_excerpt with the_content (yes, I mean to do the opposite!) in my theme?
I’d like my readers to see everything without excerpts, but can’t suss out how to replace the functions.
Tom
Is it possible to remove the image from the_content() and display only the text in single.php. In which files should I make changes? Can someone help?
Thanks, this should help me with future wordpress projects.
I am a newbie and before l I ‘ve read this post I have been working inefficiency.
Due to your post I found the solution.
I will definitely be implementing that !
Very good tutorial Justin.
Thank you.Helped me a lot.
I’ve made some searching before upgradung my blog from blogger to WP.
Your post made me the feeling that I am going to enjoy WP while there are such tools, themes, frameworks and solutions as this one.
Thanks
Hi Justin, Thanks a lot for posting this I have been looking around for this filter and just read from your post. This is really great and hope you are well. Keep on posting.
Thanks for the tutorial.
How can I modify this so that I can pass a specific word count value if I don’t want to use the default?
This will help me a lot folks.
Your post made me the feeling that I am going to enjoy WP while there are such tools, themes, frameworks and solutions as this one.
I will definitely be implementing that !
Thanks
Uhm… what is the different between content and excerpt… sounds strange for me, sory newbie right here
Thank you so much !
I will use this on my new themes.
Hi Justin,
I am changing the_content with the_excerpt on almost every blog that I make. Naturally I have to repeat it on every new theme I upload and test. If I understood the benefit of your approach correctly, by making this adjustment, every theme that I upload to my blog will automatically display excerpt instead of content on the archives, front page and search page. Did I understand it correctly?
Nice trick. But I want to the reverse way ?
Swamykant,
Reversing it (delivering the_content in the place of the_excerpt) is even easier since there’s no danger of recursive calls:
Hope this helps.
Thank you so much! I recently had to reinstall my theme due to a coding mess error in the header.php, and when I reinstalled, I noticed my home page was out of whack because my previous coding had not been saved.
I new integrating an excerpt was the only way, and this was the only set of instructions that worked perfectly for me. Thanks again!
You just saved me from editing my theme. I use child themes, so just needed to change one line of code. However, it is always better to do things with filters.