Wordpress Limit Word of a post 禁止格式化

Wordpress Limit Word of a post disables formatting

我通过创建自定义 wordpress 插件限制了我的 post 长度。但这会产生一个新问题,我的博客 posts 的布局被破坏了。

未激活插件的布局:

https://imgur.com/a/opUwzol

激活插件后的布局:

https://imgur.com/a/YsCAG6c

function limit_words_on_posts($content)
{
// Get the post content
$post_type = get_post_type();
if ($post_type == 'post'){
$post = get_post();
$url = 'https://www.vierenzestig.nl/';
$postslug = $post->post_name;

// Limit the post content
$text = $content;
$words = 300; 
$link = $url . $postslug;
$more = '...<br/><br/><strong>Wilt u dit artikel verder lezen? <a style="color: red; text- 
decoration: underline;" href="'.$link.'">Ga dan naar VierenZestig.NL!</a></strong>';


$excerpt = wp_trim_words( $text, $words, $more );

return $excerpt;
} else {
return $content;
}

}

如您所见,使用插件时换行符等已被删除。有什么我想念的吗?

wp_trim_words 从内容中删除所有标签。

您可以使用此代码:

$excerpt = force_balance_tags(html_entity_decode(wp_trim_words(htmlentities($text), $words, $more)));