我有一个Wordpress网站,它从帖子中提取文本并使用
<div class="col-md-12">
<p>
<?php
echo preg_replace('/(<img [^>]*>)/', '', get_the_content());
?>
</p>
</div>
但是这会将所有内容包装在一个 p 标签中(尽管帖子中有多个段落) 如何将
标签放回帖子中的每个段落上
尝试像这样wpautop()
:
<div class="col-md-12">
<p>
<?php
$content = preg_replace('/(<img [^>]*>)/', '', get_the_content());
$content = wpautop($content); // Add paragraph-tags
$content = str_replace('<p></p>', '', $content); // remove empty paragraphs
echo $content;
?>
</p>
</div>
get_the_content()
不会像the_content()
那样默认添加 p 标签。
阅读更多关于: wpautop()
在这里: https://codex.wordpress.org/Function_Reference/wpautop