techrounder-default

PHP Function to limit content with complete word

The following function can be used to limit the content with a complete word rather than broken words.

function casper_content_limiter( $text, $length = 64, $tail="" ) {
	$text =	trim($text);
	$txtl =	strlen($text);
	if( $txtl > $length ) {
		for( $i = 1; $text[$length-$i] != " "; $i++ ) {
			if( $i == $length ) {
				return substr( $text, 0, $length ) . $tail;
			}
		}
		$text =	substr( $text, 0, $length-$i+1 ) . $tail;
	}
	return $text;
}

In wordPress paste the above function in the function.php file in the theme folder. After that call the function from the template file or any other file where you want to limit the content by

$limited_content = casper_content_limiter( $content, 80, '...' );

Now $limited_content contains limited content.
In the function, first parameter, ‘$content’ is the actual content which wants to be limited.
Second parameter is the limit count. ie characters to be limited.
Third parameter is the string/character to be appended at the end of the limited text. here it is ‘…’.

Previous Post
techrounder-default

Function to get the categories of custom taxonomy

Next Post
techrounder-default

Video Downloader For Facebook

Add a comment

Leave a Reply

Your email address will not be published. Required fields are marked *