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.

Tagged: , ,

Author: Stuart | Website: http://stuartduff.com

Stuart is web designer and developer from Glasgow, Scotland with a passion for wordpress. Stuarts interests are tech, design, php and anything web development, open source or Internet related.

  1. jaswanth says:

    Ohh nice share friend.,

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

    1. Stuart says:

      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 says:

    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 says:

    thank you thank you thank you!

    this got me out of a jam!

  4. Yan says:

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

  5. Daniel says:

    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 says:

    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. 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 says:

    Hi,

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

    asif

    1. Stuart says:

      Hi Asif,

      The code snippet only allows you to control the length of the wordpress post excerpt, to show an image try adding the wordpress post_thumbnails code to your theme.

      http://codex.wordpress.org/Post_Thumbnails

  1. [...] 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. [...] wordpress hack was originally published on my personal blog. Tags: wordpress code snippet, wordpress content limit, wordpress hack, wordpress how to [...]

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>