Posted By

kai330 on 07/11/11


Tagged

wordpress excerpt


Versions (?)

Wordpress excerpt custom word limitation


 / Published in: PHP
 

URL: http://wordpress.org/support/topic/limit-the-number-of-words-in-excerpt-without-plugins

  1. <?php
  2.  
  3. //this part goes to functions.php
  4.  
  5. function string_limit_words($string, $word_limit)
  6. {
  7. $words = explode(' ', $string, ($word_limit + 1));
  8. if(count($words) > $word_limit) {
  9. array_pop($words);
  10. //add a ... at last article when more than limit word count
  11. echo implode(' ', $words)."..."; } else {
  12. //otherwise
  13. echo implode(' ', $words); }
  14. }
  15.  
  16. //this part goes to template
  17.  
  18. $excerpt = get_the_excerpt();
  19. echo string_limit_words($excerpt,25);
  20.  
  21. ?>

Report this snippet  

You need to login to post a comment.