Wordpress excerpt length tag


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

function that creates a template tag that can be used to insert excerpts with different lengths. Also controls characters that come after excerpt


Copy this code and paste it in your HTML
  1. // In Functions.php:
  2. function the_excerpt_max_charlength($charlength) {
  3. $excerpt = get_the_excerpt();
  4. $charlength++;
  5. if(strlen($excerpt)>$charlength) {
  6. $subex = substr($excerpt,0,$charlength-5);
  7. $exwords = explode(" ",$subex);
  8. $excut = -(strlen($exwords[count($exwords)-1]));
  9. if($excut<0) {
  10. echo substr($subex,0,$excut);
  11. } else {
  12. echo $subex;
  13. }
  14. echo "[...]";
  15. } else {
  16. echo $excerpt;
  17. }
  18. }
  19.  
  20.  
  21.  
  22.  
  23. // In wordpress template:
  24. <?php the_excerpt_max_charlength(250); ?>

URL: http://codex.wordpress.org/Function_Reference/get_the_excerpt

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.