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!
Spread this KnowTe
Digg Del.icio.us Stumble Facebook Float Technorati BumpRelated KnowTes
Subscribe
If you liked this KnowTe, why not subscribe to our RSS feed?
advertising browser chat cms coding css design email fonts forms freebies google graphics hosting icons ideas inspiration iphone javascript jquery marketing mobile office pdf photos photoshop php plugins rss search engine seo stock technology templates themes tools trends tutorials usability vector wallpaper webdesign webhosting wordpress workflow
WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 7 or better.
Subscribe to KnowTeBook
LovedKnowTes
Recommendation
Community Links
- Top 17 Websites To Easily Generate CSS Grid Layouts
Here are some great online CSS grid builders that will surely help any web developers. See which online tool will suit your needs best.
- The Best Design Related Video Content in August
Here is a wide selection and array of design based videos I have bookmarked from all over the web, be inspired and learn something new from each video listed.
- 16 WordPress Plugins for Managing Ads
In this next post, we had come up with 16 WordPress Plugins for Managing Ads.
- 10 Best Websites To Generate Buttons Online
Here you will find my collection of 10 best online button creators. Buttons are an important part of our website’s interface and these tools will help you create brilliant buttons.
- 33 Inspiring Fish Logo Designs
Here, we have collected 33 Inspiring Fish Logo Designs in which each fish shows a particular symbolic importance.
- XML 5 Star Rating System
Tutorial on how to design 5 star rating system using javascript, php and xml.
- Take Advantage of the free space available at Twitter!
Twitter is one of the best social networks available today. It is not just a social network but it has become a part of life for millions of people around the world. Recently twitter reached 20 billion tweets mark and having more than 100 million registered users. Twitter is not only used for connecting with people, but we have also noticed rather unconventional usage of it.
- July’s Fresh Photoshop Web Layout Tutorials
This roundup includes 21 Photoshop tutorials which deal with creating web layouts, fresh and inspirational resources from design community (July 2010).
- 40 Red Logo Designs for Aspiring Logo Designers
Today, we will be sharing a collection of logos which are in red color that will surely inspire the new and aspiring designers.
- Made Famous By CREAMOUS
More than just an amalgamation of all that is hip, trendy and oh-so-next-5-minutes, the CREAMOUS label is the forgotten lovechild of Fashion, Art and Design – certifiably 100% F.A.D. guaranteed.
- Check List to Evaluate PSD to HTML Service Providers
As the Internet has become a necessity for a huge population of the world and with millions of people accessing Internet space every day, and millions stay online constantly, establishing a strong online presence that has become vital for today’s businesses, non-government organizations, educational institutes or any other establishments.
- web 2.0 templatesWeb 2.0 templates with simplicity, cleanliness, and elegance in design and the less number of columns and work great for all your needs and have all the great features of Web 2.0 technology.
KnowTeBook.com
UserTalk
Social Bookmarking: I thnik social bookmarks and social networks has the main rule now.
Letminthang: I tried http://wordpress.org /extend/plugins/mine rva-wordpress/ and downloaded it easily.
vijay: This is excellent share which can be useful for all to improve the productivity…
Quero dinheiro: I did not know half of these tools. good to know that there are people willing to share
eRage website: Very nice examples! My compliments for finding these. Too bad the Nike viral is offline now. I liked...
Friends
- Business Card Designs
- Craig Farrall’s Blog
- CSS Built
- Dezignus
- Flash Mint Design Blog
- Free Photoshop Brushes
- fudgegraphics
- Fuel Your Creativity
- Just Creative Design
- Logo designs
- My Ink Blog
- NaldzGraphics
- Onextrapixel
- Pelfusion
- Photoshop Girl Tutorials
- spidanet.de
- Tasarimini | Graphic Tools
- The most stylish websites
- Website templates
- Webtoolkit4.me
Random KnowTes
- Restyle and optimise your digg count
- Easy To Use HTML Encoder
- SEO: How To Create a Google Sitemap in 5 Minutes
- How To Add License Free Images To Any website - PicApp
- How To Create A Webdesign Company Layout in Photoshop
- CODA BLUE Free WordPress Portfolio Theme
- The WordPress Domain Parking Theme
- What’s better? Ruby on Rails vs. PHP



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
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!
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…
Wordpress: Shorten the title of posts or everything else you want | mediaplana
Mar 25, 2008
ID: 2
[…] (more…) […]