Created
October 14, 2011 05:51
-
-
Save rigelstpierre/1286344 to your computer and use it in GitHub Desktop.
A quick way to shorten wordpress titles.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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