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 favourite text editor and place it in your wordpress themes folder.

function string_limit_words($string, $word_limit)
{
  $words = explode(' ', $string, ($word_limit + 1));
  if(count($words) > $word_limit)
  array_pop($words);
  return implode(' ', $words);
}

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.

$excerpt = get_the_excerpt(); echo string_limit_words($excerpt,40); 

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.

, ,

15 Responses to “Limit WordPress Posts Text Length Without The Use Of Plugins”

  1. jaswanth December 23, 2009 at 8:07 am #

    Ohh nice share friend.,

    but i am using this already but i am not getting line breaks.., just appears as a single para

    • Stuart December 27, 2009 at 2:08 pm #

      Hi jaswanth.

      Do you mean that if you add two paragraphs of length with a line break in between the line break is removed when setting the word limit? yeah

  2. schmaart January 18, 2010 at 1:03 pm #

    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:

  3. yes January 19, 2010 at 2:42 am #

    thank you thank you thank you!

    this got me out of a jam!

  4. Yan July 10, 2010 at 12:52 am #

    Thank you so much, been looking for long time ! Brillint~~

  5. Daniel August 13, 2010 at 4:11 am #

    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 ?

  6. John October 7, 2010 at 10:57 am #

    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.

  7. Arcadia Ronin November 7, 2010 at 6:38 pm #

    Thanks so much for this! After spending ages fiddling around with another example (http://rider.sofarider.com/wordpress-tips-and-tricks/how-to-trim-the_content-automatically/) I found yours and it worked perfectly straight away!

  8. asif January 7, 2012 at 12:42 pm #

    Hi,

    its working but image does not show that is in the post. please guide me how to show image as well.

    asif

  9. jimmy February 18, 2012 at 11:37 am #

    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.

    Thanks
    Jimmy

Trackbacks/Pingbacks

  1. How to limit post length on your homepage | Houston Graphic Designer - Dwayne Casey - July 3, 2010

    [...] I just needed to set this up some why not write a brief tuto­r­ial so I will remem­ber. I orig­i­nally found this at here [...]

  2. How To Limit WordPress Posts Text Length Without Plugin Wordpress Hack - August 11, 2011

    [...] wordpress hack was originally published on my personal blog. Tags: wordpress code snippet, wordpress content limit, wordpress hack, wordpress how to [...]

  3. WordPress: control the length of excerpts or comments – Shilling: The Blog - April 11, 2012

    [...] printing to screen this great little PHP snippet will do the job. I originally found this posted here on Stuart Duff’s [...]

  4. WordPress: control the length of excerpts or comments – Shilling: The Blog - April 11, 2012

    [...] printing to screen this great little PHP snippet will do the job. I originally found this posted here on Stuart Duff’s [...]

Leave a Reply