Wordpress excerpt custom word limitation


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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. ?>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.