If you are a wordpress developer and looking forward to Limit the number of characters FOR Post Titles and Post Description This is article is Heaven for you .
I Presume that You might be aware of The Templates for Home page view, Page view, Full Post View and Category view.. So, Go to the template page where you want to restrict the length of the sentence to the Specific Limit.
and copy paste the function below
function limitLength($text,$length=100) { // Change to the number of characters you want to display $chars_limit = $length; $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; }
The above function will do the Magic of reducing the string to desired character length. Now, If you want to apply this function to the Post Description
Find the Line of code in the Template page,
<div class="entry"> <?php the_content('Read the rest of this entry »'); ?> </div>
and replace it with
<div class="entry"> <?php Limit_the_content('Read the rest of this entry »'); ?> </div>
Now, we have to override the functionality of the function the_content along with Trimming away the text to desired length
to do this, just write the following function on top of the page
function limitLength($text,$length=100) { // Change to the number of characters you want to display $chars_limit = $length; $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; } function Limit_the_content($more_link_text = null, $stripteaser = 0) { $content = get_the_content($more_link_text, $stripteaser); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); // Letca call the Trimming function and limit the character length to 150 characters $content= limitLength($content,150); echo $content; }
same this for title also …
Run the Page … tada ……Works Perfect !! ( for me ) Hope It will work for you too ‘…
