How to truncate your title and your post text in Word Press with two plugins
I had a very long day today trying to track down the exact plugin combination needed to truncate both the title and text for posts in Word Press. That may sound like a weird need but I am working on this site where I need very uniform post for layout purposes and since random users can post whatever they want the only way to control height of each post was to truncate both the title and the post. Here is the site I am working on (it is still in development so a lot of stuff is broken, DON’T JUDGE ME! ;P).The plugin combination that I used is one part WP limit post automatically and one part iMP Limiter. Limit post automatically is awesome. The options are easily set in the WordPress admin panel to meet pretty much any truncating option you might need.
The second part wasn’t so easy.If you search iMP Limiter in google you will find a ton of results, but every site links to: http://www.inmypad.com/2006/12/wordpress-plugins-imp-limiter/That is a big fat 404. It turns out that this developer lost their database and you can’t download the plugin anymore from his site. So I thought for sure there was another source. There wasn’t. After like three hours I stooped to searching for open plugins directories through google, I am not going to name anyone. I went down the list of results and emailed all the contact emails on the sites. Out of the forum postings, emails to my networks and random emails to open directories I was snooping, I finally got an email back, and this kind soul provided me with the plugin. So no one else has to do this I am going to offer this plugin here:iMP Limiter (right click, save link as) I was able to find some instructions on how to use the plugin at some other random site but what was most important in the plugin were a few lines of code which decide whether you truncate the title or the post text.
The first step to using this plugin is replacing either, <?php the_content(); ?> or <?php the_title(); ?> with one of the lines of code below depending on whether you want to truncate by character count or word count.<?php iMP_Limit_Char($max_char='', $more_text=''); ?> // Use this to limit characters with default parameters.<?php iMP_Limit_Word($max_word='', $more_text=''); ?> // Use this to limit words with default parameters.
After you have added the code above to your index.php you to look below where there are two functions, one if you want to truncate based on character count and one based on word count. My code is set up to truncate the title. If you want to set it for the content you just have to replace “title” with “content” in the spots where “title” is bold.
function iMP_Limit_Char($max_char = 10, $more_text = ‘…’, $more_link_text = ”, $limit_type = ‘title‘) {if ($limit_type == ‘title’) { $limiter = get_the_title(); }else { $limiter = get_the_content(); }$limiter = apply_filters('the_title‘, $limiter);$limiter = strip_tags(str_replace(’]]>’, ‘]]>’, $limiter));if (strlen($limiter) > $max_char) {echo substr($limiter, 0, $max_char+1);echo $more_text;if ($more_link_text != '') {echo ' <a href="';echo the_permalink();echo '">'.$more_link_text.'</a>';}} else {echo $limiter;}}function iMP_Limit_Word($max_word = 40, $more_text = ‘…’, $more_link_text = ”, $limit_type = ‘title‘) {if ($limit_type == ‘title’) { $limiter = get_the_title(); }else { $limiter = get_the_content(); }$limiter = apply_filters('the_title‘, $limiter);$limiter = strip_tags(str_replace(’]]>’, ‘]]>’, $limiter));$trim = explode(' ', $limiter);if(count($trim) > $max_word) {$l = $max_word;for ($i=0; $i<=$l ; $i++) $output .= $trim[$i] . ' ';echo $output;echo $more_text;if ($more_link_text != '') {echo ' <a href="';echo the_permalink();echo '">'.$more_link_text.'</a>';}} else {echo $limiter;}}
I hope that this helps. I am not a developer/programmer I just design, code XHTML/CSS and hack stuff people have already programmed. Email me if you want, I will help as much as I can.





January 20th, 2008 at 9:28 am
Hi!
Very interesting article. Thanks for spreading the word of “WP Limit Posts Automatically”. You just gave me an idea as well:
I thought, why just be able to cut posts or pages with it when there are people that have a need for cutting the title as well.
In one of the future versions I will add a section for cutting title and another for cutting the “optional excerpt” (another thing within posts) and why not have the choice to cut comments as well?
Regards Jens Törnell (developer of WP Limit Posts Automatically)
January 20th, 2008 at 9:54 pm
Hi Jens, that sounds like an awesome idea. The more customization the better! I would love to see you update your awesome plugin with those added features. Thanks for your hard work on all your plugins!
February 17th, 2009 at 6:14 pm
I found a similiar but much shorter snippet of code at PHP Snipplr that does some simple content truncating.
I don’t know if it’s heavier because of making use of preg_spiit(), I’m not that much a PHP developer, either.
Unfortunately, both remove most of the tags and the date “iconlets” of the event calendar plugin. Maybe I’ll find something more suitable…
February 18th, 2009 at 3:58 pm
after some looking around, fiddling and merging, I got this working and it does preserve all the styles and formats and it works with [quicktags], too.
Thanks to Snipplr and AI coder!
function icba_truncate_content($excerpt_length=15, $elipsis='...', $more_link_text='') {
$content = get_the_content('', 0, ''); // $more_link_text, $stripteaser, $more_file
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
$words = explode (' ', $content, $excerpt_length + 1);
$words = array_slice($words, 0, $excerpt_length);
$final_content = implode(' ',$words);
echo $final_content . $elipsis;
}
February 18th, 2009 at 3:59 pm
oh, the $more_link_text is currently not in use. We could add a “read more” link with the permalink, but I didn’t need it.