Wet Dynamic Limit Post Excerpt Length In WordPress

Many ways to we can change WordPress post excerpt length

  • WordPress hook
  • Custom function

Add bellow snippet in functions.php file.

1. WordPress hook.

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

2. Custom function Using Number Of Words

function excerpt_limit_word($string, $limit) {
if (str_word_count($string, 0) > $limit) {
$words = str_word_count($string, 2);
$pos = array_keys($words);
$string = substr($string, 0, $pos[$limit]) . '...';
}
return $string;
}

above function to we can add limit base on word as like used limit 20 to set limit 20 words.


echo excerpt_limit_word($content, 20);

3. Custom function Using Number Of Characters


function excerpt_limit_characters($string, $limit){
if(!empty($string)){
$string = strip_tags($string);
if (strlen($string) > $limit) {
$stringCut = substr($string, 0, $limit);
$string = substr($stringCut, 0, strrpos($stringCut, ' '));
}
return $string;
}else{
return false;
}
}

above function to we can add limit base on characters as like used limit 100 to set limit 100 characters.


echo excerpt_limit_characters($content, 100);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s