How to limit post length on your homepage

I just need­ed to set this up for myself so why not write a brief tuto­r­i­al so I will remem­ber. I orig­i­nal­ly found this at here

I am repeat­ing this infor­ma­tion since I often have to do the same thing on oth­er blogs for clients and such and to fur­ther explain some points I messed up on via the orig­i­nal.

In essence to repeat what was said the code below will go into the functions.php of your indi­vid­ual theme. Uti­liz­ing CODA i log in to my serv­er and open this fold­ers path: blog > wp-con­tent > themes > your theme name >functions.php . With­in functions.php , I just cre­at­ed a space among the oth­er func­tions and past­ed the code below and saved the file — do not close the file as you may need to return to the orig­i­nal state (note that there is a com­ment added that is dif­fer­ent form the orig­i­nal):

// TO LIMIT Word­press Post length on home­page
func­tion string_limit_words($string, $word_limit)
{
  $words = explode(‘ ‘, $string, ($word_limit + 1));
  if(count($words) > $word_limit)
  array_pop($words);
  return implode(‘ ‘, $words);
}

Now In the same fold­er with­in your them locate index.php and replace the line :

<?php the_content(”.__(‘Read More &raquo;’, ‘sand­box’).”); ?>

with :

<?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,40); ?>

I orig­i­nal­ly tried to just replace the the words : the_content with all of the oth­er code. Now after a cou­ple of tries I slapped my head got it. Not too tough real­ly, and it works very well.

About the author

Dwayne Casey I’ve start­ed design­ing web­sites while at Uni­ver­si­ty of Hous­ton dur­ing my first Pho­to­shop 3.1 expe­ri­ence in 1996 on a Mac­in­tosh com­put­er. I built my own com­put­ers (Win­dows PCs) with the help of a friend and have been dig­i­tal­ly involved ever since. I am both Win­dows and PC lit­er­ate but grav­i­tate toward OSX. I’ve grad­u­al­ly got­ten back to my roots as a tac­tile artist / design­er by work­ing more with tra­di­tion­al medi­ums like draw­ing, paint­ing, and mixed media.

Leave a Reply

*

Anti-Spam Quiz:

captcha *

UA-7731317-2