Wordpress: Shorten the title of posts or everything else you want

In a recent project I needed a section with short notes (common as asides) and a list of posts in the sidebar. The problem was only, that the length of the posttitle was to long for my theme and my layout suffered under this small bug. I also didnt wanted to change the wordpress core files which might be replaced by updates in future. So I wrote a solution for this and am sharing it to the public. Alle additions are only to make in your theme. Use it for everything you want, just let the note intact or link back to this site.

Now, lets get ready:

Create or open the functions.php in your wordpress templates folder. We will add a function which makes all the work for us everytime we will need it.

FIND

<?php

ADD BENEATH

// Wordpress Hack by Knowtebook.com
// Shorten any text you want

function ShortenText($text)

{

// Change to the number of characters you want to display

$chars_limit = 100;

$chars_text = strlen($text);

$text = $text." ";

$text = substr($text,0,$chars_limit);

$text = substr($text,0,strrpos($text,' '));

// If the text has more characters that your limit,
//add ... so the user knows the text is actually longer

if ($chars_text > $chars_limit)

{

$text = $text."...";

}

return $text;

}

Now we add a little code to the place within the loop for example to cut the title. It will call the function and short the title. Take attention to the title function: get_the_title() instead of the_title(), because the_title() would give out the title directly.

FIND

<?php the_title(); ?>

REPLACE WITH

<?php echo ShortenText(get_the_title()); ?>

You can use this for any text to shorten. You just have to double the function and give it a new name, so you can have different character limits.

Have fun and share!

Share the love

BTW: Good Quality Brochures Online Color Printing at PsPrint.

Spread this KnowTe

Digg Del.icio.us Stumble Float Technorati Bump
Whatever you want to say!

Chai
Sep 22, 2010
No: 21 / ID: 6939

Very Good, It’s work on my website. Thanks So much.


Svein
Sep 16, 2010
No: 20 / ID: 6907

Thanks! I used it on the backend of markedsheltene.no! Great tip!


FerdiV
Jul 22, 2010
No: 19 / ID: 6539

Mmm this doesn’t work for me :(
I changed every ‘text’ with ‘title’ for my website and it’s not working! Nothing happens. Am I doing something wrong?

This is after the code:

while($news = mysql_fetch_array($fetch))
{echo ”
“.$news[’title’].”
“;
}

It should post the shorter title, but it doesn’t!


Crisis
Jun 03, 2010
No: 18 / ID: 6334

This worked perfectly, any chance to make it like a plugin? Where you can add different values (char_limit) for a certain tag(example post from category 1 to have a limit of 60 chars, category 2 to have 40 chars, etc) in the mode that could be easily edited something like


tornei
May 23, 2010
No: 17 / ID: 6279

On Add New Post i can write infinite characters…

how can i limit the characters (for examples 50) ?

Please help me if possible :(


tornei
May 23, 2010
No: 16 / ID: 6278

Why you don’t use it?


Aaron moritz
Apr 19, 2010
No: 15 / ID: 6003

Any way to change the code so that it will cut off mid-word, rather than always after a full word?
I’m going to be using ti for a post listing, but it’d be nice if all the category names could be uniform width, so I need them to cut off mid-word.


nico
Jan 30, 2010
No: 14 / ID: 5571

great tip, very useful.
thanks a lot


danny
Oct 19, 2009
No: 13 / ID: 5081

do you know how to do this with category names?


Janet from self help books
Jul 11, 2009
No: 12 / ID: 4506

This is cool and very easy to replace in the code - thanks for this !!


Knowtebook
Jun 26, 2009
No: 11 / ID: 4421

Hi Brenth, it is a plugin which measures the popularity of the posts according to the traffic on them.


Brenth
Jun 26, 2009
No: 10 / ID: 4420

I have a question. On your sidebar you have a section called LovedKnowTes and under it are what appears to be an archive of your posts. Is this a widget or did you hard-code it into the theme?


GenthonX
May 21, 2009
No: 9 / ID: 4299

Ups..SOLVED by myself.. there is open php tag in file functions. Thank you.


GenthonX
May 21, 2009
No: 8 / ID: 4298

Thanks for the great function. But I have problem after instal it. I can’t login to wordpress dashboard. Please help me. Thanks


kirit
May 16, 2009
No: 7 / ID: 4279

Well solution!! Works perfectly.

Thanks a lot.


Hyder
Dec 15, 2008
No: 6 / ID: 3504

Thanks for the code! Really appreciate it.


hama
Nov 26, 2008
No: 5 / ID: 3349

THANKS :D


3wlink
Nov 17, 2008
No: 4 / ID: 3247

Hi, I was looking for exactly this code. Thanks a lot for the nice and excellent solution.


Room 237: Origins
Oct 11, 2008
No: 3 / ID: 2835

This worked perfectly, and was exactly what I was looking for. Thank you!

Leave a comment!
Note: You can also use your free Gravatar avatar on Knowtebook! Please also read our Publishing Policy before posting.

Ja, ich möchte bei Kommentaren benachrichtigt werden!

Trackbacks/Pings
Trackback-URL

TechSpice
Jun 27, 2009
ID: 2

Shorten the title of posts or everything else you want…

In a recent project I needed a section with short notes (common as asides) and a list of posts in the sidebar. The problem was only, that the length of the posttitle was to long for my theme and my layout suffered under this small bug. I also didnt wan…