4

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?

1
  • 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
    Commented Jun 17, 2011 at 21:12

1 Answer 1

3

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.

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.