Custom wordpress excerpt lengths


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

Change the excerpt value in functions.php


Copy this code and paste it in your HTML
  1. //First add the filters functions
  2.  
  3. function wpe_excerptlength_teaser($length) {
  4. return 45;
  5. }
  6. function wpe_excerptlength_index($length) {
  7. return 30;
  8. }
  9. function wpe_excerptmore($more) {
  10. return '...';
  11. }
  12.  
  13. // Then the function
  14.  
  15. function wpe_excerpt($length_callback='', $more_callback='') {
  16. global $post;
  17. if(function_exists($length_callback)){
  18. add_filter('excerpt_length', $length_callback);
  19. }
  20. if(function_exists($more_callback)){
  21. add_filter('excerpt_more', $more_callback);
  22. }
  23. $output = get_the_excerpt();
  24. $output = apply_filters('wptexturize', $output);
  25. $output = apply_filters('convert_chars', $output);
  26. $output = '<p>'.$output.'</p>';
  27. echo $output;
  28. }
  29.  
  30. //The code for the template
  31.  
  32. <?php wpe_excerpt('wpe_excerptlength_index', 'wpe_excerptmore'); ?>
  33. // the other one
  34. <?php wpe_excerpt('wpe_excerptlength_teaser', 'wpe_excerptmore'); ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.