← WordPress

limitar de caracteres no titulo de um post

Lida 4050 vezes

Offline

gerardopedro 
Membro
Mensagens 596 Gostos 0
Troféus totais: 28
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1

tenho isto para ir buscar o tiltulo de um post...

Citar
<?php $recent = new WP_Query("cat=1&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>

gostaria de saber se existe alguma maneira de mostrar somente 20 caracteres desse mesmo titulo.

cumps
Offline

muiomuio 
Membro
Mensagens 324 Gostos 0
Feedback +1

Troféus totais: 23
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 4 Level 3 Level 2 Level 1 100 Posts 50 Posts

O código que aí tens vai-te buscar 1 post da categoria de id 1 não o titulo. O que te mostra o titulo é a tag the_title()

Podes usar PHP e o substr para restringir o número de caracteres. Acho que fica algo tipo substr(the_title(), 0, 25);

Se isto não der podes sempre experimentar definir uma variável para receber o valor do titulo

Código: [Seleccione]
$titulo = the_title();
substr($titulo, 0, 25);

e já deve dar.
Offline

Paulo Faustino 
Membro
Mensagens 735 Gostos 2
Feedback +2

Troféus totais: 29
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1

Citar
      <a href="<?php the_permalink() ?>" rel="bookmark"><?php $shorttitle = substr(the_title('','',FALSE),0,25); ?><?php echo $shorttitle; if (strlen($shorttitle) >24){ echo '&hellip;'; } ?></a>

Abraço :cool:
Offline

gerardopedro 
Membro
Mensagens 596 Gostos 0
Troféus totais: 28
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1

Citar
      <a href="<?php the_permalink() ?>" rel="bookmark"><?php $shorttitle = substr(the_title('','',FALSE),0,25); ?><?php echo $shorttitle; if (strlen($shorttitle) >24){ echo '&hellip;'; } ?></a>

Abraço :cool:

boas...

inseri isso mas deu erro....
Offline

anjo2 
Membro
Mensagens 3020 Gostos 0
Troféus totais: 31
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Poll Starter Level 5 Level 4 Level 3 Level 2

Citar
      <a href="<?php the_permalink() ?>" rel="bookmark"><?php $shorttitle = substr(the_title('','',FALSE),0,25); ?><?php echo $shorttitle; if (strlen($shorttitle) >24){ echo '&hellip;'; } ?></a>

Abraço :cool:

boas...

inseri isso mas deu erro....
Código: [Seleccione]
<a href="<?php the_permalink() ?>" rel="bookmark"><?php $shorttitle the_title('','',FALSE); if(strlen($shorttitle) > 25$shorttitle substr($shorttitle,0,22)."..."?><?php echo $shorttitle; if (strlen($shorttitle) >24){ echo '&hellip;'; } ?></a>
Offline

gerardopedro 
Membro
Mensagens 596 Gostos 0
Troféus totais: 28
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1

Citar
      <a href="<?php the_permalink() ?>" rel="bookmark"><?php $shorttitle = substr(the_title('','',FALSE),0,25); ?><?php echo $shorttitle; if (strlen($shorttitle) >24){ echo '&hellip;'; } ?></a>

Abraço :cool:

boas...

inseri isso mas deu erro....
Código: [Seleccione]
<a href="<?php the_permalink() ?>" rel="bookmark"><?php $shorttitle the_title('','',FALSE); if(strlen($shorttitle) > 25$shorttitle substr($shorttitle,0,22)."..."?><?php echo $shorttitle; if (strlen($shorttitle) >24){ echo '&hellip;'; } ?></a>

e nada
Offline

gerardopedro 
Membro
Mensagens 596 Gostos 0
Troféus totais: 28
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1

só se estou a fazer mal...

tenho que substituir certo???
Offline

muiomuio 
Membro
Mensagens 324 Gostos 0
Feedback +1

Troféus totais: 23
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 4 Level 3 Level 2 Level 1 100 Posts 50 Posts

Comigo o código em cima funcionou perfeitamente apenas com copy/paste.

O que eu fiz foi substituir:

Código: [Seleccione]
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
por

Código: [Seleccione]
<a href="<?php the_permalink() ?>" rel="bookmark"><?php $shorttitle the_title('','',FALSE); if(strlen($shorttitle) > 25$shorttitle substr($shorttitle,0,22)."..."?><?php echo $shorttitle; if (strlen($shorttitle) >24){ echo '&hellip;'; } ?></a>
Offline

tenhaseublog 
Membro
Mensagens 1 Gostos 0
Troféus totais: 14
Trófeus: (Ver todos)
Super Combination Combination Level 3 Level 2 Level 1 First Post Karma Seventh year Anniversary Sixth year Anniversary Fifth year Anniversary

Abraço :cool:
Valeu Paulo, como sempre suas dicas fazem valer seu sucesso no mundo dos blogueiros, utilizei esta dica para diminuir a exibição dos títulos e funcionou perfeitamente....