limit-title-content

Limit WordPress Post title and content output by the amount of characters

Posted in Web Design/Development, Wordpress | No Comments

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();

}

}
Now all you need to do is call the relevant function and specify the amount of characters you want:
<?php
// Limit title by 10 characters
short_title(10);

// Limit content by 20 characters</div>
short_content(20);

?>


No Comments to “Limit WordPress Post title and content output by the amount of characters”

Comments are closed.

Recommended

Expert Help with WordPress
Learn how you can market your business on Twitter
Get the Book now! Use 25% discount code 'november25'
Hostgator - Cheap and Reliable Web Hosting - WordPress friendly

Incoming Tweets