Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.
share|improve this question
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 '10 at 15:42
add comment

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
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 '10 at 19:26
add comment

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.

share|improve this answer
    
Thanks Hakre. I'll try it out! –  Tal Galili Oct 7 '10 at 19:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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