A post excerpt is a description or summary that we show to readers to get their interest in reading.
A title may sometimes be not enough to grab the attention of readers, and it doesn’t convey the quality of content that well.
So at the various places of your website, like in the blog page, in the homepage, and in the widgets where your posts are placed, excerpts play their role in getting the interest of your readers. When we don’t want to show the full content we use post excerpt.
In a WordPress theme, it is mainly used in following ways:
- RSS Feeds
- Search Results
- Tag Archives
- Category Archives
- Monthly Archives
- Author Archives
By default WordPress shows a limit of 55 words in an excerpt. However, you can customize this limit in many ways.
To show a post summary simply use the_excerpt() below post title.
In this post, I will show you…
Special Discount for SmallEnvelop Readers and Subscribers
Using my link, you will get 25% off on all InVideo plans
(Offer valid till 30th April)
Grab The Discount
A Number of Ways To Limit & Control Post Excerpt Length
- Limit post excerpt length or post content length using number of words.
- Limiting excerpt length to number of characters.
- Limit post summary by adding ‘read more’ tag.
- Enabling custom excerpt to write your own summary for each post.
- Control Excerpt Length using Filters
Let’s learn to do each one of these.
1. Limit Post Excerpt Length Using Number Of Words
Add following code in your functions.php file.
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`[[^]]*]`','',$excerpt);
return $excerpt;
}
function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/[.+]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
Now, in every place where you use the_excerpt()
or the_content()
in your loop, use excerpt($limit)
or content($limit)
.
For example if you want to limit your excerpt length to 30 words use echo excerpt(30)
and for content.
Source: Bavotasan
2. Limiting Excerpt Length To Number of Characters.
Sometimes you want to limit the post excerpt limit on character and not by words. You just want to limit the character length in excerpt.
Here is the solution. Add the following code to your function.php
file
function get_excerpt(){
$excerpt = get_the_content();
$excerpt = preg_replace(" ([.*?])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 50);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = $excerpt.'... <a href="'.get_the_permalink().'">more</a>';
return $excerpt;
}
You can change the length 50 to your desired character length. And also ‘more’ to what text you want to display. Then call your function <?php echo get_excerpt(); ?>
wherever you are planning on getting your posts.
You may also be interested in these articles:
3. Limit Post Summary By Adding read more
Tag.
You can also set your excerpt length by adding more
or read more
tag using post editor tag option. Simply place the blinking text cursor where you want the excerpt to be stopped and click the ‘more’ tag.
4. Enabling Custom Excerpt To Write Your Own Summary
Sometimes you might not satisfied with automatically generated excerpt, which might be breaking your site layout.
There is a feature in WordPress to show an additional field for custom excerpt.
This feature isn’t activated by default but very simple to enable for any post. Click on Screen Option found above the title in post editor.
By enabling it there is a new field ‘excerpt’ can be seen below the content editor, as you can see in the image above. Now enter your custom excerpt with your desired length to fit your site layout.
5. Control Excerpt Length Using Filters
Excerpt length is set to 55 words by default. We can change this default value without overriding default the_excerpt() function, so that you don’t have to make a change in each files and templates where you have used the_excerpt().
To change this default excerpt length to 20 words using excerpt_length filter, add the following code to functions.php file in your theme:
function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
This way you can limit post excerpts length.
Hope this article may help you to limit post excerpt length. Comments are welcome.
Bonus: Canva is an excellent tool to design blog images, social banner, business cards, posters, infographics, resumes, and other visual graphics. It has a very user-friendly drag and drop interface that makes your job easy, so anyone can easily create beautiful graphics.
Canva also provides pre-made templates for posters, logos, cards, resumes, flyers, powerpoint presentations, and many more. It’s a wonderful tool that designers and non-designers both can use to create beautiful and appealing images.
You may also refer:
Great work !
Thanks
This method also works very good:
<?php
echo substr(get_the_excerpt(), 0,100)."…";
echo'ID) . ‘”>lees meer‘;
?>
You won’t see this, but GENIUS! Thank you for shortening my agonizing search for a solution.!
Wow! thanks!
Excellent Job…Well done dear..!! 🙂
Thank you for commenting!
Hi,
I’m suggesting a CSS trick for do something like this.. 🙂
p{overflow: hidden; text-overflow: ellipsis; white-space: nowrap;}
Thanks,
http://www.w3sanju.in
It will limit your paragraph to single line. Generally used for title.
Found an excerpt counter online.
Useful when writing the excerpt text.
How would I change the length of the excerpt only in widgets? While not changing the length within regular posts.
you can limit the length of the widget using the widget text filter
function smallenvelop_text_replace($text) {
return substr($text, 0, 100) . ‘…’;
}
add_filter(‘widget_text’, ‘smallenvelop_text_replace’);
Here 100 is the word count you can change this as per your requirement.
Thanks for your reply. Unfortunately all of the text disappears when I use this function. Any idea why? Or something else I could try?
Perhaps I could have worded this differently – the excerpt I’d like to change the length of is in a sidebar – can I do something like this? (this doesn’t work)
`function custom_excerpt_length($length) {
if (is_dynamic_sidebar(‘left’)) { //For the left sidebar
return 20;
} else {
return 100; //for all others locations
}
}
add_filter(‘excerpt_length’, ‘custom_excerpt_length’);`
Hello, i’m sorry to up this post but i have a question, i would like change the length of the excerpt and i use this function $article_nvt->post_excerpt();
how i can change the length of the excerpt with this function ? (if you need i can put more code)
Hello Shewolf,
You can use above codes because they depend on post_excerpt() function. Don’t worry and use the solutions.
Nice writing , I was fascinated by the analysis – Does anyone know where I could find a blank Geneseo Return to Work Form: Medical Authorization copy to edit ?
Greetings Mac Shows . my partner acquired a template a form document with this link
Howdy! I’ve used the suggested code from #2 on my blog, but I’m having some issues with the Read More snippet. It seems to just link back to the home page of the blog, not the permalink (the href of the read more)
Is ‘.$permalink.’ referencing something I don’t have built into my functions? OR what is going haywire here. Thanks so much for the snippet, it’s just what I was looking for!
The site for reference is http://www.catchthecudas.com
– Mark
I want to use more then 100 thousand word in a wordpress post, how can possible this, please explain.
Hello SebaNews
There is no limit on how much words are there in a single post, but you might get the problem with PHP’s memory limit on the server.
The easiest way to change this is by editing your php.ini file, but some hosts won’t allow this so you’ll probably need to contact them.
The default limit was 8MB before PHP 5.2, 16MB in 5.2 and 128MB afterwards.
Hope this link may be useful to you
https://wordpress.org/support/topic/cannot-publish-long-pages-word-count-limit
That’s it! Thanks for the article. It works.
very useful info for me
Only wanna input that you have a very decent website, I like the
style it really stands out.
Thanks Curtis 🙂
Thanks, Poonam I will try this code to our website cgfrog.com which using schema theme, I was searching it for a long time. Hope it works.
Some genuinely interesting information, well written and generally user pleasant.
Thanks a lot saved my life 🙂
Thank You Solve This Problem. I’m very happy for your nice post.
Glad you enjoyed it. Keep reading 🙂
Hi,
I am having an issue with the function. I entered the code for reducing the excerpt size as it is into the functions.php (btw it says function.php on the top). After that when I substitued the_excerpt with excerpt(30) assuming that it will give me 30 words from my post instead of 55, it didn’t work. Any idea why? pasting below the code…
ORIGINAL
have_posts() ) : $queryxxx->the_post(); ?>
<div class="listpages “>
<a href="”>
<a class="morelink" href="”>
REVISED
have_posts() ) : $queryxxx->the_post(); ?>
<div class="listpages “>
<a href="”>
<a class="morelink" href="”>
Any idea what am I doing wrong?
I think there was an issue with the code I put up in my previous post. So putting it here:
ORIGINAL: https://codeshare.io/IooMo
REVISED: https://codeshare.io/RW6I8
Hello Abhishek,
use
and please ensure that you are using the code in the right place.
Is there a way I can shorten it just on the Category Page of my site, rather than in the standard post RSS?
Hello Ryan,
You can modify the excerpt length on the category page by using filters.
Use the below code, it will definitely solve your problem.
function custom_excerpt_length( $length ) {
if ( is_category() || is_archive() ) {
return 20;
}
else{
return 55;
}
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
Please let me know if you have any problem.
Thanks
Poonam Namdev
Thanks for sharing!
I think there might be an error in the second method, in this line:
$excerpt = trim(preg_replace( ‘/s+/’, ‘ ‘, $excerpt));
that will strip some “s” from the text.
so I changed to:
$excerpt = trim(preg_replace( ‘/\s+/’, ‘ ‘, $excerpt));
hope this helps!
Thanks Poonam all were cool tricks, specially last one.
Very interesting, thanks 🙂
thank you very much it works
I decreased my wordpress excerpt length to 50 characters. Its works and amazing.
Thanks.. but the full stops are removed from the excerpt plus @Massimiliano helped get the s back into the text. How do i get the fullstop to print?
for some reason i still use trik Nu. 4, because i can manage what to show in excerpt, but after all.. the trik is amazing, and will try it on my other blog… thankyou Sister Poonam for this Triks… GBU
Very good. Just what I was looking for 😉
Thanks for this very useful post! Is there a way to change the excerpt length depending upon category?
For instance, I have a blog where I’d like the excerpt to be 100 words, except for just one category where I don’t want any excerpt (0 words — just the title will show).
Can’t quite figure out how to do that.
Hello Debbie,
You can modify the excerpt length for the specific category by using filters.
Use the below code, it will definitely solve your problem.
function custom_excerpt_length( $length ) {
if ( is_category('category_id')) {
return 0;
}
else{
return 100;
}
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
PS: Change the “category_id” with that specific category id and let me know if you have any problem.
We have started a WordPress forum where we will provide help for any WordPress related issue, so please feel free to ask any question.
http://forum.smallenvelop.com/
Thanks
Poonam Namdev
Thanks, Poonam! That’s just what I was looking for! 🙂
Thanks, it works like a charm 🙂
Thanks.. It was very useful.
Thank you, it worked!
Thank you for your post. Really looking forward to read more. Much obliged.
Thanks very much, this is very helpful!
Hi Poonam,
I don’t understand the line
$excerpt = preg_replace(‘`[[^]]*]`’,”,$excerpt);
What is it purpose?
Thanks,
James
Thank you for the tips. I was having a hard time keeping the html code from woocommerce excerpt and limiting it. Worked like a charm.
Use wp function wp_trim_words
https://developer.wordpress.org/reference/functions/wp_trim_words/
Parfait bel article, bravo!
Hello. Nice site !
Perfect, was my solution.
I m trying to only display one category of posts on the front page of my WordPress site. How can I change this?
That works. Many thanks, it was my headache for some time now the problem is gone
THANK YOU! Was messing with the excerpt for awhile before reading your post. Very helpful.
Thanks man, it works.