How to only show the first X words (from each post) on the home page?

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.

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.

Leave a Comment