admin管理员组

文章数量:1123505

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.

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.

Share Improve this question edited Aug 23, 2016 at 6:41 winchendonsprings asked Aug 23, 2016 at 5:31 winchendonspringswinchendonsprings 1333 silver badges9 bronze badges 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. – Ethan Rævan Commented 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. – winchendonsprings Commented Aug 23, 2016 at 6:43
Add a comment  | 

3 Answers 3

Reset to default 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; ?>
<?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

I am using this:

in your functions.php

function custom_trim_content_with_html($text){
    $length = 1250;
    if(strlen($text)<$length+10) return $text;//don't cut if too short

    $break_pos = strpos($text, ' ', $length);//find next space after desired length
    $visible = substr($text, 0, $break_pos);
    $visible = strip_tags( $visible, array('<p>')); // allow p tag

    return force_balance_tags($visible) . "…";
}

in your loop or single post code:

echo custom_trim_content_with_html(get_the_content());

本文标签: Trim content without stripping formatting