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!
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
But how would this 50 word limit work with tags and images?
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..
@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.
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
@Yan: You’re welcome, I’m glad if this recipe helped you
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.
@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) ?
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.
@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!)
Hey Jean
For some weird reasons, the code doesn’t display anything. Am I missing something here?
Yan
@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
@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.
@Randy: This is excellent! Thanks a lot for that trick! (And for the compliment
)
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
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
@Yan: I’ll probably write a recipe about adding gravatars to your comments, so stay tuned!
@Drew Douglass: Thanks a lot
ini yang aku cari2
this is what am i looking for
byme
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?
@Ronny: This recipe isn’t optimized to display images within the excerpt. Personally, I use a custom field to display images near the excerpt.
Nice trick, go to practice ….
Great tip…
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.
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!
@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
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
@Paulo Brito: This code must be used within the loop, this is why it don’t works on your sidebar I think.
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
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?
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: