I've tried

<?php $trimmed = wp_trim_words( get_the_content(), 55, "" ); ?>
<?php echo $trimmed; ?>


<?php $trimmed = wp_trim_words( the_content(), 55, "" ); ?>
<?php echo $trimmed; ?>

the second option with the_content does not work as expected.

Some posts have a video in the content and when the formatting is stripped the embed code shows.

EDIT: I'll try to be more clear, the author has written posts with images, links to external images, videos, text, formatted wysiwyg text, etc. all in the main content field of a normal post. So all those things I just mentioned exist in the_content.

What the author has NOT done is used the excerpt field, or the tag and created really long posts. So I'm trying to create a page of those posts that are trimmed.

  • May you provide an example so I can understand what the expected result should be? This way i can also test the function on my end. Aug 23, 2016 at 6:10
  • @EthanJinksO'Sullivan I've editted my question a little with more detail. I'm creating a page with 10 normal posts that are trimmed in length. But still retains some formatting. Aug 23, 2016 at 6:43

2 Answers 2


Well, as far I understood you want to remove all the HTML tags from the trimmed the_content. Right???

Try wp_filter_nohtml_kses function. Hope that's going to work. Here is the full code-

<?php $trimmed = wp_filter_nohtml_kses( wp_trim_words( get_the_content(), 55, "" ) ); ?>
<?php echo $trimmed; ?>
  • 1
    This doesn't trim the_content for me. It prints the whole entire post contents. I think you need to pass wp_trim_words get_the_content not the_content. Am I wrong? Aug 23, 2016 at 7:55
  • Yes, you need to use get_the_content(). When I copied your code snippet from above, accidentally I copied the wrong one. :) Now I've edited the answer. Please check it. By the way if you think this answer helped you please accept it. @winchendonsprings
    – CodeMascot
    Aug 23, 2016 at 8:17
  • What I'm trying to do is retain formatting ( keep it ) but still trim the post. With this bit of code it leaves escape characters. So if you have the word "it's" it will now be it\'s . Also code in the body content like [video][/video] will remain. Aug 24, 2016 at 18:03
  • I tried the above code with get_the_excerpt. No way to get the trim work. It does nothing. And researching the web there are those terrible long and complex solutions… Come on, this can’t be so hard!
    – Garavani
    Jun 7, 2022 at 8:12
<?php echo  force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( wpautop(get_the_content()) ), 40 ) ) ); ?>

This code working for me to Trim content without stripping formatting

  • very unlikely that you remove random space delimited words and it works as intended. all balancing tags do is to make sure the html is valid, doesn't mean it do not lose attributes at the last element trimmed Jan 11, 2023 at 9:08

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.