Advertisement
miriamdepaula

WordPress: Truncando textos (limitando caracteres)

Nov 9th, 2011
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.83 KB | None | 0 0
  1. <?php
  2. /* limita a quantidade de caracteres do excerpt - deve ser colocado no arquivo functions.php */
  3.  
  4. function limitatexto($texto, $final, $limite){
  5.     $result = $texto;
  6.     $len_texto = strlen($texto);
  7.     $len_final = strlen($final);
  8.        
  9.     if ($len_texto + $len_final > $limite){
  10.         for ($i=$limite-$len_final;$i!==-1;$i--){
  11.             if (substr($texto, $i, 1) == " " and substr($texto, $i-1, 1) !== " "){
  12.                 return substr($texto, 0, $i).$final;
  13.                 break;
  14.             }
  15.         }
  16.     }
  17.     return $texto;
  18. }
  19.  
  20. ?>
  21.  
  22. <?php //MODO DE USAR: no template, onde quer que seu texto seja afetado...?>
  23.  
  24. <?php
  25. //Ao invés de usar <?php the_excerpt()?>, vamos usar "get_the_excerpt()" ....
  26.  
  27. $excerpt = get_the_excerpt();  
  28. echo limitatexto( $excerpt, '[...]', 80); //80 é a quantidade de caracteres...
  29. ?>
  30.  
  31. <a href="<?php the_permalink()?>">Leia mais &raquo;</a>
  32.  
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement