Yan asked: “How to limit the size of the post excerpt without using a plugin?”

Yan, who’s a regular reader of my blogs, asked me how he can limit the length of the post excerpt without using a plugin. Come on, my friend, I got what you need!

Nothing hard here. Edit your single.php file and replace the the_excerpt() function by the following code:

<?php
$len = 50; //Number of words to display in excerpt
$newExcerpt = substr($post->post_excerpt, 0, $len); //truncate excerpt according to $len
if(strlen($newExcerpt) < strlen($post->post_excerpt)) {
    $newExcerpt = $newExcerpt."[...]";
}
echo "<p>".$newExcerpt."</p>"; //finally display excerpt
?>

Let's have a look at the code: First, we define the desired number of words for the post excerpt. It is defined to 50 in this example. Then, we use the php substr() function to get only the 50 first words of the excerpt.
We verify that our new excerpt isn't shorter than the excerpt recorded in the database. If it is, we add [...].
And finally, we display our 50 words long excerpt.

32 Responses

Oct 25 2008 20:03

But how would this 50 word limit work with tags and images?

Oct 25 2008 21:18

thanks for code,
i wonder if it is possible to limit content with lines instead of the number of words?
for sometimes content might be given as list and so excerpted post still might be long than we expect..

Oct 26 2008 03:10

@Ronny: You can use this code in any WordPress loop, so you can use it in your archive.php file as well. About images, what do you mean?

@eddai: lines length depends of your theme and your screen resolution, so I don’t think there’s any way to do that. Therefore, the problem with a content given as list is really annoying. I’ll post a recipe if I find anything to do against this problem.

Oct 26 2008 12:43

Hey Jean

That’s very fast and I’m surprised that you choose to address the question in a post but I’m sure there are others who might be interested to know how to do it as well.

I’ll be working on it shortly and will keep you updated. Thanks, buddy.

Yan

Oct 26 2008 12:45

@Yan: You’re welcome, I’m glad if this recipe helped you :)

Oct 26 2008 23:29

Can I limit excerpt by character? because in Thai language, we don’t use space to seperate word. Space only separate sentence. If I use that code, it will see 1 Thai sentence = 1 word
(Now I’m using plugin to limit excerpt by character)

by the way, I guess Ronny ask something about whether it counts the html tag, or html (img) tag stripping in excerpt, which I want to know too. I want to display image and embedded media in except but most of the time it doesn’t work because the html tag isn’t stripped properly so I ended up go to text-only excerpt.

Oct 26 2008 23:48

@SE7EN: Sure, it is possible to modify this recipe to limit the size of the excerpt by character. I can write a recipe about that if you’re interested!
Aboout html tags in the excerpt, I don’t think it is possible without hacking core files, which isn’t recommended. For the images, why not using a custom field (like I do on this blog) ?

Oct 27 2008 05:23

wp_trim_excerpt formatting.php line 849

create a new one with your specification. remove the old, add the new. or create a new one that use can use with conditionals in your loop.

remove_filter(‘get_the_excerpt’, ‘wp_trim_excerpt’);
add_filter(‘get_the_excerpt’, ‘your_new_trim_excerpt’);

just dont mess with the core, it makes upgrades something to be feared.

Oct 27 2008 08:51

@Randy: Thanks for submitting some code here! But personally, I’d prefer not to edit any core file (As you said, it makes upgrading something to be feared!)

Oct 27 2008 11:46

Hey Jean

For some weird reasons, the code doesn’t display anything. Am I missing something here?

Yan

Oct 27 2008 12:04

@Yan: Are you sure you pasted the code within the loop? If yes, I should have a look at your theme file. Email me if needed :)

Oct 27 2008 15:46

@jbj Actually you can use that those two filter deals and a completely new except function in your functions.php file and never touch the core. it’s pretty nice, if you really want to mess with your excerpt a lot.

by the way, I love what you do here. :-)

Oct 27 2008 15:48

@Randy: This is excellent! Thanks a lot for that trick! (And for the compliment :) )

Oct 27 2008 19:42

I got around it using a plugin before so I’ll probably stick with now for a while.

Now it’s me again with another question: How do you change the default gravatar keeping in mind that I’m using Brian’s threaded comment? Yet another article for you. With me around, you will never short of recipe ideas…;)

Yan

Oct 28 2008 01:50

Just wanted to say thanks for a cool trick and awesome new site. I have been lurking for a while now, I love this site :)

Oct 28 2008 16:41

@Yan: I’ll probably write a recipe about adding gravatars to your comments, so stay tuned!

@Drew Douglass: Thanks a lot :)

Nov 02 2008 13:49

ini yang aku cari2
this is what am i looking for
byme

Nov 10 2008 09:15

I mean, will HTML tags and images be stripped, the two last words in my excerpt is “<img src”, will it break the image, show the HTML or what? :)

Nov 10 2008 10:08

@Ronny: This recipe isn’t optimized to display images within the excerpt. Personally, I use a custom field to display images near the excerpt.

Dec 03 2008 14:10

Nice trick, go to practice ….

Dec 04 2008 04:55

Great tip…

Dec 07 2008 11:05

anyway to get the caption code out of there using this code? thanks it works great, now my client doesn’t have to remember to paste something in the excerpt field.

Dec 16 2008 08:25

But how can I limit my post with a photo embed.I mean my post have photo in the right side corner.I want to limit my post with 50 words with photo at the right side.Can you help me.Please.

Thanks guys!

Dec 16 2008 08:35

@Joel: It’s is really simpler to use a custom field for the picture. I don’t have any solution right to get an image that way :(

Dec 24 2008 05:07

I’m trying use the code on the sidebar but the code doesn’t display anything. How can i solve the problem. The page is at http://www.oitavaesfera.net/parateste

Dec 24 2008 10:04

@Paulo Brito: This code must be used within the loop, this is why it don’t works on your sidebar I think.

Jan 02 2009 00:37

Hi!

I was trying to use this recipe to implement the same thing for my blog, but we don’t have excerpts there. How can I edit this code to do the same for the teaser of the posts on my front page?

Thanks

Feb 01 2009 15:33

Hi, the code doesn’t display anything for me either. Here’s my code

<a href=”" rel=”bookmark” title=”Link to “><img src=”/images/” alt=”" id=”leadpic” />

post_excerpt, 0, $len); //truncate excerpt according to $len
if(strlen($newExcerpt) post_excerpt)) {
$newExcerpt = $newExcerpt.”[...]“;
}
echo “”.$newExcerpt.”"; //finally display excerpt
?>

Any other reason why it wouldn’t show up?

Sep 17 2009 14:39

I’ve been trying to pull articles from WP using
$returns = mysql_query($query);
$result = mysql_fetch_row($returns);
$post_title = $result[5];
$post_excerpt = $result[7];
but the actual article doesn’t display
Andrew

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required