How to Limit Post Excerpt Length In WordPress

0
45
how to limit the excerpt in wordpress

How to Limit Post Excerpt Length In WordPress

how to change excerpt length in WordPress in a number of ways. Now you can customize the length of this part of the text according to the particular needs of your articles.

As a WordPress user, you know how different themes can change the structure of articles, including the excerpt display. However some of the default settings really don’t fit the style and the tone of your articles.

Now you don’t have to depend on those settings. The WordPress excerpt length can be customized.

Limit your WordPress Excerpt Length to a certain number of words Using PHP

If you want to set a word limit for your excerpt, use the 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;

}

You should pay attention to the_excerpt() and the_content() segments. Write into the brackets the number of words you want to limit your excerpt to.

 

Limit your WordPress Excerpt Length by the number of characters using PHP

Another solution is to change the criterion to the number of characters. In WordPress, all you have to do is to use the following code in the setting file of your theme, no matter if it’s a classic or child theme.

/**

 * Limit excerpt to a number of characters

 *

 * @param string $excerpt

 * @return string

 */

functioncustom_short_excerpt($excerpt){

            returnsubstr($excerpt, 0, 200);

}

add_filter('the_excerpt', 'custom_short_excerpt');

200 is the number you have to replace. Choose any other number of characters that suit your needs.

Change excerpt length with filters

There is also an easier way for you to automatically change the excerpt length without editing each file. Instead of including and editing the_excerpt() function, you can use filters.

WordPress has a very efficient filter, called “excerpt_length”. In order to enable it, copy the following snippet into your functions.php file. Then change the “20” to the number of words to which you want to limit your excerpt.

add_filter( 'excerpt_length', function($length) {

return 20;

} );

Beforehand, set your theme to run the the_excerpt function. If you use a premium one, you don’t need to follow that step, because those themes are already prepared for the function.

 

Change excerpt length using Custom Excerpt Length Plugin

This one works on the same principle – simplicity and efficiency. Check the Settings after installing the plugin and you’ll see the new option “Reading page”. Just set the number of words.

 

Change excerpt length using Advance Excerpt  Plugin

This plugin is a little more sophisticated. Apart from the basic option of changing the excerpt length, it allows you to add some more advanced functions. Check it out and see how it can improve the general display of your blog.

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments