Limit WordPress Posts Text Length Without The Use Of Plugins

Limit Wordpress Posts Text Length Without The Use Of Plugins
Stuart Avatar

Published on

< 1

min read

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.

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.





11 responses to “Limit WordPress Posts Text Length Without The Use Of Plugins”

  1. jaswanth Avatar

    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 Avatar

      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 Avatar
    schmaart

    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 Avatar
    yes

    thank you thank you thank you!

    this got me out of a jam!

  4. Yan Avatar
    Yan

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

  5. Daniel Avatar

    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 Avatar
    John

    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 Avatar

    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 Avatar
    asif

    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 Avatar

      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

  9. jimmy Avatar
    jimmy

    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

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.