WordPress Limit Description Characters lenght


#1

Hi Friends i am using Author widget plugin. This echo author biographic info in sidebar.
The Author description part is also showing in sidebar i want limit description character length and put readmore text in next line. currently description echo now through this code:

 <?php If ($author->description) : ?>
  

<?php Echo nl2br($author->description)?>

<?php EndIf ?>

Need Help Friends.


#2
webdesignerart said

Hi Friends i am using Author widget plugin. This echo author biographic info in sidebar.
The Author description part is also showing in sidebar i want limit description character length and put readmore text in next line. currently description echo now through this code:

 <?php If ($author->description) : ?>
  

<?php Echo nl2br($author->description)?>

<?php EndIf ?>

Need Help Friends.

echo substr($author->description,0,strpos($author->description,' ',30));

you could limit to 30 chars.


#3

This will truncate to a specified # of characters, but not in the middle of a word, and append your custom html to the end

     function fs_truncate($content,$length,$after='...') {
		// rather than just use substr, let's make sure we cut it at the end of a word
		$arr = explode(' ', $content);
		$out = '';
		$count = 0;
		foreach( $arr as $str ){
			$count += ( strlen($str) + 1); // +1 is for the space we removed
			if ($count > $this->length)
				break;
			$out .= $str . ' ';
		}
		// make sure we got SOMEthing
		if (!$out) {
			$out = $arr[0];
		} 
		$out = trim($out);
		$out .= $after;
		return $out;
	}

#4
chrismccoy said
webdesignerart said

Hi Friends i am using Author widget plugin. This echo author biographic info in sidebar.
The Author description part is also showing in sidebar i want limit description character length and put readmore text in next line. currently description echo now through this code:

 <?php If ($author->description) : ?>
  

<?php Echo nl2br($author->description)?>

<?php EndIf ?>

Need Help Friends.

echo substr($author->description,0,strpos($author->description,' ',30));

you could limit to 30 chars.

I use this code its works for me :slight_smile: and thankx for your time.

  <?php $author_desc = nl2br($author->description);
echo substr($author_desc,0,150).'[...]'; ?>

#5
webdesignerart said

I use this code its works for me :slight_smile: and thankx for your time.

  <?php $author_desc = nl2br($author->description);
echo substr($author_desc,0,150).'[...]'; ?>

Use Chris’s code, so you don’t cut your text in the middle of a wo…
:slight_smile: