Sometimes you want to include a list of recent posts or posts in a category and you don’t want them to go onto a second line, so the only way to get around this is by shortening the title. This can be done using a handy little function that you put in your theme’s functions.php file.
Simply copy and paste the following into your functions.php file:
function short_title($after = '', $echo = true, $length = false) { $title = get_the_title(); if ($length && is_numeric($length)) { $title = substr( $title, 0, $length ); } if (strlen($title)> 0) { $title = apply_filters('short_title', $title . $after, $before, $after); if ($echo) echo $title; else return $title; } }
And to call this function within your theme, use the following code:
<?php short_title('...',true, '42'); // short_title('AfterText', 'true', 'WordLength') ?>