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.
1 2 3 4 5 6 7 | 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.
1 | < ?php $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.
7 Comments › Leave yours
2 Trackbacks
- You are now listed on FAQPAL - Limit Wordpress Posts Text Length Without The Use Of Plugins...The code snippet below should be placed somewhere within your wordpress ...
- How to limit post length on your homepage | Houston Graphic Designer - Dwayne Casey - [...] I just needed to set this up some why not write a brief tutorial so I will remember. I ...
Leave a Reply
Search this Site
Stuart on Twitter
- RT @WebDesignDev: ^^ Twitter Tricks And Tips To Get More Followers: http://bit.ly/9xzQcZ ^^
- is watching http://bit.ly/aXBu4W
- is watching http://imdb.to/cKv5D0
- is watching http://bit.ly/bxJpKN
- loved Human Traffic on Boxee http://imdb.to/1FSIzV
Recent Comments
- laiptai on 50 How To Create Design WordPress Themes Web Layouts In Photoshop Tutorials
- Paul on Top 25+ WordPress Anti Spam Protection And Management Plugins
- Daniel on Limit WordPress Posts Text Length Without The Use Of Plugins
- Lola Allen on Are WordPress Premium Themes Becoming Too Heavy and Bloated With PHP Code
- Yan on Limit WordPress Posts Text Length Without The Use Of Plugins




Ohh nice share friend.,
but i am using this already but i am not getting line breaks.., just appears as a single para
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
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:
thank you thank you thank you!
this got me out of a jam!
sometimes it is hard to write original articles for blogs but i always seems to manage creating fresh original content for blogs,;-
Thank you so much, been looking for long time ! Brillint~~
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 ?