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.

Is it possible to call the_excerpt() with tags intact? I'd like to create a excerpted listing of my posts in a specific category, but I'd also like the links and formatting carried over from the post content.

I'm currently using the_excerpt() which otherwise works fine, however, the tags are stripped out.

I can't find a filter to place on the_excerpt() to do this, so barring that, is it possible to filter the_content() to pull the first 100 words with tags and a read more link at the end?

share|improve this question
    
this is virtually impossible - think of all the possible html tags within the content; a: this makes counting the words very difficult; b: stopping after a certain number of words will likely break in the middle of one or many html tags. it might be possible within reason, if you can restrict the html tags that you need to preserve. you would still need to run some code to close open html tags. –  Michael Jun 17 '11 at 21:12

1 Answer 1

up vote 3 down vote accepted

Actually, I just did something like this for a Drupal site. I based my truncation function on this: Truncate text preserving HTML tags with PHP

Use the final version of the function at the end of the comments. The function takes its $length parameter in characters, not words, but you can probably use the general rule-of-thumb of 5 characters per word to estimate, if needed.

Hook your function to the get_the_excerpt filter, and you should be in pretty good shape.

share|improve this answer

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.