Blog for web designers

How to increase or decrease excerpt length in wordpress

With the help of wordpress function we can decrease and increase the defalut excerpt length. By default the excerpt length is set to 55. Also when using excerpt length wordpress placed the [….] string at the end of any paragraph. We will also see that how we can remove it to overcome neat and clean paragraph text only. Its very easy with few lines of codes.
If you want to change the length no. simply change the value of return 20 to something you desire.

How to increase or decrease excerpt length in wordpress.

Paste the below into your function.php

function custom_excerpt_length( $length ) {
	return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

In this way you can just adjust the excerpt length to your own requirement. But you can see that the last word of the excerpt is truncated. You will see [….] the string.

Remove [….] string from the end of paragraph.

You will see the [….] at the very end of the paragraph where you have use the excerpt length. WordPress keeps that by default. We can use wordpress function to remove the charater and add the new character we want.
Now below code will help you to remove the [….] string from the end of the paragraph.
Remove [….] string from the end of excerpt line and creates a blank space.

function new_excerpt_more( $more ) {
	return '';
}
add_filter('excerpt_more', 'new_excerpt_more');

Removes the [….] string from the end of paragraph and adds the “more..” text with the permalinks set.

function new_excerpt_more( $more ) {
        $permalink = get_permalink($post->ID);
	return '<a href="'.$permalink.'">more..</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');

However, You can customize it to your own needs.

Custom excerpt length without truncated

function get_excerpt($count){
  $permalink = get_permalink($post->ID);
  $excerpt = get_the_content();
  $excerpt = strip_tags($excerpt);
  $excerpt = substr($excerpt, 0, $count);
  $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
  return $excerpt;
}

We have cover how to increase or decrease excerpt length in wordpress.

Santosh Shah

My name is Santosh Shah and blogs at santosh-shah.com


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.