By Jornes


2012-10-30 04:47:12 8 Comments

Possible Duplicate:
excerpt in characters

I have a question after reading this post (How to highlight search terms without plugin). I like this function(Search Term Without Plugin) very much but the character length is too long. What php code should i add to make the excerpt shorter? Would appreciate if someone can suggest it. Thank you!

2 comments

@Adam 2012-10-30 08:36:23

In addition to the above filter hook supplied by Deepa's answer here is one additional function that can help you extend the use of the_excerpt in two ways,

Allows you to...

Limit the excerpt by number of characters but do NOT truncate the last word. This will allow you to return a maximum number of characters but preserve full words, so only the words that can fit within the specified number limit are returned and allow you to specify the source of where the excerpt will come from.

function get_excerpt($limit, $source = null){

    $excerpt = $source == "content" ? get_the_content() : get_the_excerpt();
    $excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, $limit);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
    $excerpt = $excerpt.'... <a href="'.get_permalink($post->ID).'">more</a>';
    return $excerpt;
}

/*
Sample...  Lorem ipsum habitant morbi (26 characters total) 

Returns first three words which is exactly 21 characters including spaces
Example..  echo get_excerpt(21);  
Result...  Lorem ipsum habitant 

Returns same as above, not enough characters in limit to return last word
Example..  echo get_excerpt(24);    
Result...  Lorem ipsum habitant  

Returns all 26 chars of our content, 30 char limit given, only 26 characters needed. 
Example..  echo get_excerpt(30);    
Result...  Lorem ipsum habitant morbi
*/

This function can be used multiple times through out theme files, each with different character limits specified.

This function has the ability to retrieve an excerpt from either,

  • the_content
  • the_excerpt

For example, if you have posts that contain text in the_excerpt box on the post editor screen, but want to pull an excerpt from the_content body instead for a special use case you would instead do;

get_excerpt(140, 'content'); //excerpt is grabbed from get_the_content

This tells the function that you want the first 140 characters from the_content, regardless of whether an excerpt is set in the_excerpt box.

get_excerpt(140); //excerpt is grabbed from get_the_excerpt

This tells the function that you want the first 140 characters from the_excerpt first, where no excerpt exists, the_content will be used as a fallback.

The function can be improved to be made more efficient and or incorporated with the use of WordPress filters for both the_content or the_excerpt or simply used as is in situations where there is no suitable, in-built WordPress API alternative.

@Jornes 2012-11-02 05:24:05

Hi! Thanks for all for the answer provided! I would like to ask, how to make it work with ... instead of [...] at the end of excerpt?

@Adam 2012-11-02 05:36:43

The last line, $excerpt = $excerpt.'... <a href="'.get_permalink($post->ID).'">more</a>'; is what you can use to define your "read more" link so to speak. You can see there it adds an ellipsis but you can add whatever you like.

@AlbertSamuel 2018-07-20 07:26:41

@Jornes it maybe 6 years late, but here is the HTML code for the ellipsis &hellip;

@Jornes 2019-05-10 06:42:46

@AlbertSamuel Thank you for the answer. :)

@dipali 2012-10-30 05:10:18

add these lines in function.php file

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

@Ionut 2016-12-07 15:48:51

This limits the number of words to 20, not the characters.

@Navnish Bhardwaj 2018-04-11 07:49:03

Why we have added number 999 here?

@Annapurna 2018-04-18 06:33:59

@NavnishBhardwaj 999 is the priority for the filter to be loaded. refer here for more details. developer.wordpress.org/reference/functions/add_filter

Related Questions

Sponsored Content

6 Answered Questions

[SOLVED] How to control manual excerpt length?

  • 2011-01-04 11:58:15
  • hannit cohen
  • 17740 View
  • 6 Score
  • 6 Answer
  • Tags:   excerpt

2 Answered Questions

Highlight search words in excerpt

  • 2017-07-20 15:47:58
  • ksqInFlightM
  • 1075 View
  • 0 Score
  • 2 Answer
  • Tags:   search excerpt

1 Answered Questions

[SOLVED] Limit length of first excerpt in the loop

1 Answered Questions

[SOLVED] Custom excerpt length WooCommerce product

2 Answered Questions

[SOLVED] Change excerpt length of first post

  • 2015-12-03 14:08:42
  • James
  • 406 View
  • 1 Score
  • 2 Answer
  • Tags:   excerpt

3 Answered Questions

[SOLVED] Limit the size of the post excerpt

  • 2011-10-27 18:21:08
  • user8503
  • 3718 View
  • 0 Score
  • 3 Answer
  • Tags:   excerpt

2 Answered Questions

[SOLVED] Character Limit w/ Excerpt removes Images Why?

2 Answered Questions

[SOLVED] Continuum Theme - Excerpt length

  • 2012-04-26 00:39:15
  • Isaac Boda
  • 316 View
  • 0 Score
  • 2 Answer
  • Tags:   excerpt

1 Answered Questions

[SOLVED] How to change the length of excerpt() method?

  • 2012-02-01 11:18:19
  • Ahmad
  • 121 View
  • 1 Score
  • 1 Answer
  • Tags:   excerpt

Sponsored Content