Wordpress Excerpt Dynamic Length


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



Copy this code and paste it in your HTML
  1. function excerpt($limit) {
  2. $excerpt = explode(' ', get_the_excerpt(), $limit);
  3. if (count($excerpt)>=$limit) {
  4. array_pop($excerpt);
  5. $excerpt = implode(" ",$excerpt).'...';
  6. } else {
  7. $excerpt = implode(" ",$excerpt);
  8. }
  9. $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
  10. return $excerpt;
  11. }
  12.  
  13. function content($limit) {
  14. $content = explode(' ', get_the_content(), $limit);
  15. if (count($content)>=$limit) {
  16. array_pop($content);
  17. $content = implode(" ",$content).'...';
  18. } else {
  19. $content = implode(" ",$content);
  20. }
  21. $content = preg_replace('/\[.+\]/','', $content);
  22. $content = apply_filters('the_content', $content);
  23. $content = str_replace(']]>', ']]>', $content);
  24. return $content;
  25. }
  26.  
  27. <?php echo excerpt(25); ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.