5 Easy Ways To Limit Post Excerpt Length In WordPress

,
A post excerpt is a description or post summary that we show to readers to get their interest into reading. Title may sometimes be not enough to grab the interest 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 post are placed, experts 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:


  1. RSS Feeds
  2. Search Results
  3. Tag Archives
  4. Category Archives
  5. Monthly Archives
  6. 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.

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

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.


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="'.$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 echo get_excerpt();  wherever you are planning on getting your posts.

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.


Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.