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 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 facebook fonts freebies google graphics hosting icons ideas inspiration iphone javascript jquery marketing mobile office pdf photos photoshop php plugins rss search engine seo social media stock technology templates themes tools trends tutorials twitter usability webdesign webhosting wordpress workflow
WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 7 or better.
Subscribe to KnowTeBook
Loved KnowTes
- SEO: Create Advanced Sitemaps with A1 Sitemap Generator
- 20+ Amazing Video Apps for Twitter
- Create and Share Reviews Using New Adobe CS Review
- WordPress: Custom CSS stylesheets for Posts or Pages
- 100+ Brilliant Free Go Green Eco Friendly Vector Graphics and Icons
- Web Strategy: Get real-time comments built for the social web with Lifefyre
- iPhone App for Designer: Proportions Calculator by Erik
- What Does Google Wallet do for Mobile Marketing?
- Combine Wordpress, PHPBB and 4Images together without multiple user-registrations
- Facebook vs. Google: How Marketers Can Learn Out Of the Recent Smear Campaign?
- Social Media: How to Create Automatic If This Then That Tasks
- Selected Free Wallpapers That Rock - Volume 1
- 350+ Free eCommerce Icons for Web Design Projects
- Convert your Image To Vector Online For Free
- 80 Free Mobile Device Icons Including BlackBerry Special
- Free iPad Vector GUI Elements for Adobe Illustrator
- UI Inspiration: A Showcase of Beautiful User Interfaces
- 56 Free Vector Arrow Symbols & Icons
- Free Hand Drawn Social Media Icon Set as Vector for Bloggers
- 10 Awesome And Creative Google+ Brand Pages
- 20 Amazing Mobile Friendly Tips and Tools for your Website
- Icons: Free Social Media Icon Set With Bubbles
- Seven Big Mistakes Of A Twitter Addict
- Free Photoshop Case Templates For DVD, CD and Cassette
- Go Google: Socialize Your WordPress Blog without Using Plugins
- 8 Free Brand Logo Download Websites You Should Know
- Styling Website Comments: Correct Markup and Free Examples
- Brilliant 3D Flash Image Slider For Websites
- 35 Free Beautiful Japan Theme ICONS
- 125 FREE Oldschool Pattern Swatches for Illustrator
- How To Create a Retro Wallpaper
- 8 Heatmap Tools and Why / How to use them
- Introducing New RSS Generation - What is pubsubhubbub?
- 10 Hot Facebook Plugins For Wordpress
- 28 Free Twitter Background PSD Templates
- Impressive Ways to Present Your Portfolio or Media Files
- Mega Drop-Down Menu Inspiration
- 160+ Brilliant Web Design Templates & Tutorials
- 11 Hot Tips and Tricks to Speed Up Wordpress
- Semantic Wordpress - Automatic Semantic Metadata for Your Content
- 800+ Free HTML Newsletter Templates for Startups
- Hot Trends and Insights On Web 3.0
- How To Build a Clean Modern Website Design From Scratch
- Analyzing A Trademark Brand: Absolut Vodka
- Inspiration: What If Google Ruled The World
- 72 Questions to Ask New Web Design Clients
- Free iPhone Design Set for Photoshop
- How To Easily Move or Export Your Thunderbird Mails to Outlook
- How To Add License Free Images To Any website - PicApp
- Awesome Slideshow jQuery Plugin For Portfolios with SlideDeck
- Gpanion - A new google workspace with all your apps
- How Workstations Of Designers and Bloggers look like
- Small and Accessible News Slider in JQuery
- 17 Free Brilliant Photoshop Browser Templates
- 20 web-based business collaboration tools that lets make you breathless
- Free Icons: Digg, Twitter, Stumble, RSS, Delicious & Reddit
- Use A Body ID To Style Your Website In CSS
- How To Create A Webdesign Company Layout in Photoshop
- Old eMail - New eMail: Google ReNews Email Communication
- GIVEAWAY WINNERS - 3 x Fanurio Time Tracking License
Community Links
- 20 Mind Melting Examples of Light Painting
Photographers utilize their knowledge of the different colors produced by various lights, and how they interplay with white balance settings. Light paint brushes can be as simple as a flash light, laser pen, old lantern, or even off-camera flash combined with colored gels.
- KP Design
Inspiration for all - photography, design, art, travels, and more!
- 30 Free Christmas Icon Sets for Designers
Christmas is just around the corner and this is the time when designers need loads of different resources and material to design Christmas related themes,
- Domain Name Selection: How to choose the best
To have an appropriate domain name is always the cause of worry for most website owners, the reason being, it is the primary step to ensure the success of a website or an online business. But there are several other benefits of having a right domain name such as it helps in effective marketing, branding, attracting visitors, building credibility, etc.
- 60 Remarkable Examples Of Typography Design #6
It’s been a really long time since we did our last installment series of “Remarkable Examples Of Typography Design.” To be precise, the last post was way back in June, 2011. Christmas is right around the corner and many designers are using their creativity at a maximum expense.
- Make Forms More Usable With HTML5
html5 introduces new form elements, new input types, and new attributes to help making building and using forms easier.
- Examples of Street Art and Graffiti Art
With today’s collection, we aim to pay tribute to some of the best street art that will simply blow your mind away.
- Showcase of Beautiful iPhone Wallpapers
Acknowledging the fact that the design and development community love to decorate their devices with stylish and classy wallpapers,
- Beautiful Tera Environment Concept Art & Character Design
Developed by Korean game developer Bluehole Studio and published by Frogster who also manage Runes of Magic, these environments are breathtaking with some being just concept art and not the final version they show that the world will be beautiful. In addition featuring detailed character design I can not wait for the release of Tera next year.
- 25 Incredible Logo Designs Inspired by Plant
Logos are the visual representation of the organizations and their businesses,
- How To Add Images To Your CSS Borders
How to use css3 border-images, instead of generic styles, as borders around your elements.
- 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
спортни стоки: That gives me ideas what to do with mine. :)
BestBit: Very interesting free tool, but if you have a big site, with more than 500 pages, you have to pay for it.
andrew: cool
Randy: I am thinking of using Lifefyre but maybe for now, I’ll use Disqus. I have a friend from peak...
Niringalpha: Wow, this is soooooo cooool . Guy’s if ya want to seek out cool wallpapers , take a peek out...
Friends
- Business Cards Online
- Craig Farrall’s Blog
- CSS Built
- Design Inspiration
- Design Resources
- Design Tutorials
- 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
- Vector Pack
- Wallpapers
- Website templates
- Webtoolkit4.me
Random KnowTes
- Icons: Free Social Media Icon Set With Bubbles
- How to Relaunch KnowTeBook? Join the discussion!
- Suchmaschinen Optimierung mit dem Top Ten Analysis SEO Tool
- Office: Businessplanung mit Online-Tool der IHK
- Wordpress: Reach any admin page in one click with Admin Drop Down Menu
- Self-Marketing: Outstanding Portfolio Ideas in 3D
- CMS: Open source php cms without database
- Webentwicklung: Blogs vergleichen mit WeblogMatrix


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