wpGeek

How to limit WordPress posts title

During my work as a wordpress theme coder I came across that there are request for a function on how to limit the post titles. So here you are the code and hope that it will be useful for you:

Open functions.php in your theme and copy/paste this code:

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

Then just put this function where you want in your wordpress theme templates:

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

Note:

  • Number in the gaps means to how much symbols you want to limit
  • The function should be used in the post loop
On Jul 31, 2009 Posted by and it is filed under: Tutorials

11 Responses for "How to limit WordPress posts title"

Thanks for this tut

Hi,
This is such a great resource that you are providing and you give it away for free. I enjoy seeing websites that understand the value of providing a prime resource for free. I truly loved reading your post.
Thanks!

Mamunur Rasid
oDesk Outsourcing Provider.

Hi,Thanks for the nice post!
I am having some problems related to title..
how to put image or photo after or before title post in wordpress blog.
I have a few articles on my home page and I would like to make the post articles clickable. Right now my post titles are not clickable. I am using WordPress for my site.
I want to name the page title (Page Title) different from the Post Title. If I name the page title something really optimized but the post title something short and sweet would this make my site less SEO optimized then naming the post title the same as the page title?
Please do reply!

wpGeek

not sure if I understand your question can you email me?

hi,
how can I make the title to appear shorter only in the post (for example 10 letters), but in the head to remain the same ?!

Hey guys,
I’ve been struggling with a similar issue for a day or two, how to limit the length of a post title in wordpress..? Well I’ve got as far as it will limit what is displayed on the screen, but the problem is it is not updating or changing the original length of the title. Whilst this is not such a problem on my site, it becomes a mojor problem if I submit a post to Twitter for example.

Does anyone know how to get the_title in wordpress to update to the truncated version of the title that gets displayed on the screen..?

Thanks Steve

wpGeek

well Steve, if you are making the post to automatically post on twitter then the title is taken from wordpress database, for a solution of yours I think plugin could be made.

[...] of the day? Limiting the characters in post titles on the index.php page. Resources: thislinkhere and [...]

Hi thanks for giving good code for the_title function …

post_title) > 30) {
echo substr(the_title($before = ”, $after = ”, FALSE), 0, 30) . ‘…’;
} else {the_title(); } ?>

I used this code. still work. but How could I put that on the widget area. I used already a plugin that can read php. but I still not work. anyone can help me? thanks.

oh, this code rather

post_title) > 30) {echo substr(the_title($before = ”, $after = ”, FALSE), 0, 30) . ‘…’;
} else {the_title(); } ?>


Leave a reply