WordPress: Function to Shorten Posts’ Titles

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') ?>
Free Graphics, Icons, Tutorials
Free Graphics Free Christmas Vector Icon Graphics Pack 2017Free Fitness Vector Icons Graphics PackFree Camping Vector Graphics PackFree Summer Graphics PackFree File Icon PackFree Fast Food Vector Graphics
Sharing is caring...
Like & Follow
Share the love, share this page! Facebook Twitter Digg Reddit LinkedIn Pinterest Email
Close [X]
The Web Taylor
1000 Lakeside North Harbour Portsmouth, Hampshire PO6 3EN
02392 123358