How can you have it so that the site's home page will only show the first X (let's say 300) words of the post?

But without using "more" tag, or hand filled excerpts? I am looking for a plugin/hack for WP 2.9 and onward.

I came across several solutions so far, but am hoping for a recommended solution.

Challenges I came a cross so far:

  • What happens in case a tag (for example ) starts on word 295, and ends after word 301 ?
  • Can it be possible to have a different X for the home page , tags page, category page - and so on?
  • Can the format of the text be preserved? (all the images and text formating)?
  • Having the plugin take the least amount of recourses from the server.
  • 1
    If you want to preserve the format, the get_first_words($html_source, $number_of_words) function must support HTML but cutting a table into half because X number of words had been matched is not always fitting. htmlpurifier.org might be of help or TIDY which comes with a tag parser, as well as domdocument.
    – hakre
    Aug 24, 2010 at 15:42

3 Answers 3


Changing the word count on the home page is easy:

if( is_home() )
  add_filter( 'excerpt_length', create_function( '', 'return 300;' ) );

Just replicate that code and change the conditional check to add this to other pages. The other option is to just insert the code on the template page (home.php, tag.php, etc.), so you know it's going to be set on the correct page.

Using the_excerpt() will automatically strip shortcodes and html from the content if there's no excerpt provided. You can remove these filters, but it makes it much much harder to do word counts when you're adding markup into the mix. If you want the formatting/text/images preserved, that's what the more tag is for. It's inserted manually because it's too difficult to automatically figure out in all instances where that break should go.

  • Thanks John, I hope that Hakre solution will be enough. But if not, maybe I'll combine it with your solution. Cheers, Tal
    – Tal Galili
    Oct 7, 2010 at 19:26

You can use the Fancy Excerpt Plugin For WordPress for that and modify it a bit. It preserves the style, does word-counting. You just need to modify this a bit to have your word-count and maybe if you don't like the ellipsis to modify the ending as well.


if you just need the first 300 words of the content you can easily get it with

wp_trim_words( $post->post_content, 300, '');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.