Have you ever wondered how authors are able to display the post title or excerpt with instances where its based on the amount of characters instead of words? All you have to do here is paste the code below into your functions.php file.
// Limit Post Title by amount of characters
function short_title($num) {
$limit = $num+1;
$title = str_split(get_the_title());
$length = count($title);
if ($length>=$num) {
$title = array_slice( $title, 0, $num);
$title = implode("",$title)."...";
echo $title;
} else {
the_title();
}
}
// Limit Post Content by Amount of Characters
function short_content($num) {
$limit = $num+1;
$content = str_split(get_the_content());
$length = count($content);
if ($length>=$num) {
$content = array_slice( $content, 0, $num);
$content = implode("",$content)."...";
echo '<p>'.$content.'</p>';
} else {
the_content();
}
}
<?php // Limit title by 10 characters short_title(10); // Limit content by 20 characters</div> short_content(20); ?>