How to limit the characters on post title

thumb How to limit the characters on post titleSometime, especially news type of contents, titles of post can be long and mess your neat layout. This is how to limit the post titles.


1. Copy and paste this code in your function.php

<?php
function limit_title($title, $n){
	if ( strlen ($title) > $n ){
		echo substr(the_title($before =, $after =, FALSE), 0, $n) . ‘…’;
	}else{ 
		the_title();
	}
}
?>

2. To use this function simply change the number of characters and paste it to your template file in where you want to limit the characters on post title.

<?php limit_title($post->post_title, 30); ?>

Thanks to WP Geek for this code

Author: Takashi Irie

Takashi Irie is an experienced commercial web designer and Wordpress expert based in London. He is very passionate about web design, typography, grid systems as well as Wordpress.

Enjoyed this post? Share it!

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Twitter
  • email
  • RSS

3 Comments

wpthemes  on December 14th, 2009

thanks for code

Husien adel  on December 24th, 2009

thanks a lot Takashi , I was searching on that trick for a wp client theme modification and found it , Nice blog ..Keep Going Man .

BTW i discover your Blog Via My friend Google :D

alex-ye  on July 5th, 2010

thank you very much thats very helpfull !!!

Leave a Comment