Skip to content

Instantly share code, notes, and snippets.

@rigelstpierre
Created October 14, 2011 05:51
Show Gist options
  • Save rigelstpierre/1286344 to your computer and use it in GitHub Desktop.
Save rigelstpierre/1286344 to your computer and use it in GitHub Desktop.
A quick way to shorten wordpress titles.
function ShortenText($text) {
// Change to the number of characters you want to display
$chars_limit = 100;
$chars_text = strlen($text);
$text = $text." ";
$text = substr($text,0,$chars_limit);
$text = substr($text,0,strrpos($text,' '));
// If the text has more characters that your limit,
//add ... so the user knows the text is actually longer
if ($chars_text > $chars_limit) {
$text = $text."...";
}
return $text;
}
Shorten Wordpress Titles
function ShortenText($text) {
// Change to the number of characters you want to display
$chars_limit = 100;
$chars_text = strlen($text);
$text = $text." ";
$text = substr($text,0,$chars_limit);
$text = substr($text,0,strrpos($text,' '));
// If the text has more characters that your limit,
//add ... so the user knows the text is actually longer
if ($chars_text > $chars_limit) {
$text = $text."...";
}
return $text;
}
RAW
To use this code, copy and paste into your function.php in wordpress and find <?php the_title(); ?> and replace with <?php echo ShortenText(get_the_title()); ?>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment