Limit Wordpress Posts Text Length Without The Use Of Plugins

Limit WordPress Posts Text Length Without The Use Of Plugins

Sometimes when creating WordPress themes you might want to automatically limit the length of post text that is displayed in the main page post teaser view to make things easier for clients when writing articles. You could use a plugin like limit-post etc but I always find it much easier to contain all these type of modifications within the themes own functions.php file. This little WordPress hack will allow you to limit the displayed post text by a defined number of words on your blogs home page.

The code snippet below should be placed somewhere within your WordPress themes functions.php. If your WordPress theme does not have a functions.php file simply create one with your favorite text editor and place it in your WordPress themes folder.

Now to automatically limit the number of words displayed in your post text you simply add and edit the code below to you WordPress theme. Usually this will replace the_content() code within your themes index.php and any other sections on your blog where you would like to limit the teaser text output like archives, search etc.

To control the number of words displayed on your blogs home page from the main article post you can simply edit the 40 within the second piece of code. 40 means this will only show 40 words so if you set it to 50 it will show 50 words.

Within your single.php you should leave the_content() code in place otherwise the full article text will not show within post pages.

13 thoughts on “Limit WordPress Posts Text Length Without The Use Of Plugins

  1. I think @jaswanth means that this function also removes the tags that normally surround the_excerpt(), and also strips out any paras in the_content(). My workaround is probably clumsy but surronds with tags as follows:

  2. I already tried this this function, but my image disappear. maybe function excerpt limit hyperlink to be show. anybody know how to display hyperlink into the main page with limit function ?

  3. Dude, spent a whole day tearing my hair out over how to do this. Then we found this article, followed it and everything just worked. Straight to favourites.

  4. Hi

    I want to show 200 characters of my posts which i created under a category. The posts are coming fine but if i try to use php function ‘substr or strlen’ it show me 0. please help.


Leave a Reply

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