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.
4 Comments › Leave yours
1 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 ...
Leave a Reply
Search this Site
Stuart on Twitter
- @wptaverncan you imagine dealing with every customer support request by email each day \o/ at least with a forum old answers solve issues
- @wptavern it's a very competitive marketplace and if you enter it without a great support network your going to have headaches in my opinion
- @wptavern cool see it now :) their support setup looks ropey to me? fill in a form seriously? at bare mimum i would expect forum support.
- @wptavern do you know if they are following the GPL approach on their themes like others have done, Couldn't see any info on their site.
- @wptavern didn't notice that. strange i would just have used ejunkie for everything, maybe they don't like the ejunkie aff settings.
Recent Comments
- hack on A Cool Little Wordpress Hack To Help Protect Your Blog From Comment Spammers
- Lawrence Krubner on WPQuestions A Paid Problem Solving Site For Wordpress
- You are now listed on FAQPAL on Limit Wordpress Posts Text Length Without The Use Of Plugins
- Brad Mahaffey on Are Wordpress Premium Themes Becoming Too Heavy and Bloated With PHP Code
- Brad Mahaffey on Why I Am Being Forced By VirginMedia’s New Spying Tactic Cview DPI To Encrypt My Overall Internet Connection




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!