0

I've tried

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

and

<?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.

2
  • 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 '16 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 '16 at 6:43
1

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; ?>
3
  • 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 '16 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 '16 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 '16 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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