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 twitter usability wallpaper webdesign webhosting wordpress workflow
WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 7 or better.
Subscribe to KnowTeBook
LovedKnowTes
- Community Link Feed
- 8 Free Brand Logo Download Websites You Should Know
- Free Design Portfolio Theme for WordPress: ImPress
- Pattern Freebies: 26.000+ Free Photoshop Patterns for any use
- Free Simple Wordpress Portfolio Theme
- 35 Free Beautiful Japan Theme ICONS
- Free Photoshop Case Templates For DVD, CD and Cassette
- Free Wordpress Theme for Designer and Portfolio Websites
- Convert your Image To Vector Online For Free
- 34+ free HTML newsletter templates for startups
- 56 Free Vector Arrow Symbols & Icons
- Cool Business Cards That People Will Never Throw Away
- How To Create a Retro Wallpaper
- Freebies: Twitter Bird Icons & Illustration
- Graphics: 9 Free Vector PNG Icons
- 125 FREE Oldschool Pattern Swatches for Illustrator
- Graphics: 66 free logo vector templates for your business
- Free vector brand logotypes in CDR, EPS and WMF formats
- Graphics: 15 000 free high quality icons
- Free Hand Drawn Social Media Icon Set as Vector for Bloggers
- Selected Free Wallpapers That Rock - Volume 1
- 8 Heatmap Tools and Why / How to use them
- Graphics: Free Smashing Vector Icon Set - Milky
- 80 Free Mobile Device Icons Including BlackBerry Special
- Free Icons: Digg, Twitter, Stumble, RSS, Delicious & Reddit
- CMS: Open source php cms without database
- Free iPhone Design Set for Photoshop
- Clever Flickr Open Source Flash Image Gallery For Your Website
- Free iPhone style Photoshop Icons
- What's better? Ruby on Rails vs. PHP
- CSS: How to make a footer which really stays on the bottom
- 20 web-based business collaboration tools that lets make you breathless
- Creative Online Ad Design: Ideas & Trends in 2008
- Create a realistic Credit Card in Photoshop
- Ambient Media Premium Advertising Inspiration
- Free Unique WordPress Magazine Theme: ZinePress
- 6 Awesome Wallpapers for PC, MAC and Iphone
- CSS Revolution - Use Condition Rules WITHIN Styles
- 148 free beautiful vector graphics for web and print
- How To Promote Your Posts in 10 Minutes to 1 Mio People
- The Most Stunning Viral Ad Campaigns Ever
- 8 Free Daily Calender Icon Set for Blogs
- PDF-Dokumente kostenlos aus Microsoft Word, Excel oder Powerpoint erstellen
- Graphics: 15 Free Outstanding Icon Sets for iPhone
- 25 graphic resources for ornaments, fleurons, and more
- 28 Free Twitter Background PSD Templates
- 5 Free Stunning Avatar Icons in PSD Format
- Von Word zu PDF kostenlos in 3 Minuten
- Combine Wordpress, PHPBB and 4Images together without multiple user-registrations
- Most Awarded Advertisements in 2008
- CODA BLUE Free WordPress Portfolio Theme
- Free Vector Graphics at Vecteezy
- Interesting Sites
- Graphics: Great Free Icons & iphone Wallpaper at Pixelgirl
- SEO: How To Create a Google Sitemap in 5 Minutes
- Graphics: 2000+ Open Source Web Templates
- Self-Marketing: Outstanding Portfolio Ideas in 3D
- Free Brilliant Website Chats and Why / How to use them
- Wordpress: How to use one installation for multiple Domains
- 1.000 More Free High Quality Icons For Download
Recommendation
Community Links
- iPhone Games
Best place to get updated information related to iPod Touch apps and iPhone games
- Community News: Are you Missing out in Submitting your Design Articles Online? | Design Juices
Articles can be submitted in nearly every design site around, each will often either have their own page or have a submission form in the sidebar of their pages. News can be submitted quick and easily, often bringing new droves of people to your site.
- Desire Lines: Let Your Audience Shape Your Design
Desire lines show how people are actually using your design. They show how people want your website to behave.
- Learn a New Photoshop Trick with VunkySearch
One of the best ways to learn a new trick in Photoshop is to grab a tutorial that catches your eye and dive right in. VunkySearch offers just that kind of eye-pleasing, pick-up learning.
- Jump-Start Your Freelance Career
Practical methods of getting new freelance business
- The Inside Design
Web and Graphic Design Blog
- Images Of Faces: What Do They Communicate?
Three principles of design that involve how we perceive images of people are attractiveness bias, face-ism ratio, and baby-face bias.
- All Da Crazy and Fun Stuffs You Can Do in PHOTOSHOP
Because a little bit craziness is healthy for your life. And there are no boundaries to what you can do in Photoshop. So fire up your imagination and let’s check out what life has got to offer today.
- Gotta' love HTML5 & CSS3
Creating a tabbed animated image gallery with only HTML & CSS
- Missile Defense Agency Logo – A new twist to the tale!!
Here comes a new twist to the Missile Defense Agency logo design. A striking resemblance has also been observed between the new “MDA logo design” and the “Iranian Space Agency logo design”.
- Cognitive Dissonance: How Contradictory Ideas Affect Design
The theory of cognitive dissonances suggests that people are motivated to reduce dissonance by changing or rationalizing attitudes and beliefs.
- 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
Contabilidade em Geral: Congratulations!!!
James: Thanks for these tips. Although my blog does not have the traffic yet to require such measures, I believe...
Utah Seo Inc: I’ll have to check and see if my sites even have sitemaps on them, I don’t think the free...
Jade Adtoon: The picture was amazing! I can’t imagine if I could see a flying car at our backyard. Well, this...
dan: You cannot believe how long ive been googling for something like this. Browsed through 9 pages of Google...
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



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…) […]