set character limit on the_content in wordpress

“the_content” is one of the most used function of wordpress. This function is use to get content for post and pages. This function must used inside a loop. Some time we are using “the_content” in wordpress and we want to fetch the first few characters of the content. Mostly we use “the_excerpt” for this kind of result. “the_excerpt” used to fetch first few line of the content and after that it creates an anchor tag which links to full content of the post. “the_excerpt” mostly gets 55 characters but you can change it according to your websites requirements. You can  read more about excerpt in this article.

Today in this tutorial we will get the content of an article using “the_content”. As i Mentioned earlier this is not the replacement of excerpt. In some cases you don’t want to use excerpt and you want to fetch data directly from content. So if you don’t want to use Excerpt then you can use this code to get your results. Please follow guidelines and before making any changes kindly make backup for your template files.

Open your theme folder and edit that file here you want to add “the_content” with character limits. So just find “the_content” in your template and replace it with this code.

$newcontent= get_the_content();
$newcontent=substr($newcontent, 0, 300); //add desired characters instead of 300
$newcontent=strip_tags($newcontent); // remove all html tags from content
echo $newcontent;

As you can see in the Example I use three hundred character limit. You can use according to your requirements. I use another function “strip_tags”. Its used to removed any font style or HTML tags applied on the content. If you don’t want to use this function. you can skip this step. So that is all. I hope you will get the results according to requirements. Feel free to post comments and questions if You have in my mind.